Spring?Boot實(shí)現(xiàn)文件上傳下載
本文實(shí)例為大家分享了Spring Boot實(shí)現(xiàn)文件上傳下載的具體代碼,供大家參考,具體內(nèi)容如下
示例【Spring Boot 文件上傳下載】
程序清單:/springboot2/src/main/resources/templates/register.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>注冊(cè)</title> <link rel="stylesheet" th:href="@{css/bootstrap.min.css}" > <link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" /> <script type="text/javascript" th:src="@{js/jquery-1.11.0.min.js}"></script> <script type="text/javascript" th:src="@{js/bootstrap.min.js}"></script> </head> <body> ?? ?<div class="panel panel-primary"> ?? ??? ?<div class="panel-heading"> ?? ??? ??? ?<h3 class="panel-title">Spring Boot文件上傳</h3> ?? ??? ?</div> ?? ?</div> ?? ?<div class="container"> ?? ??? ?<div class="row"> ?? ??? ??? ?<div class="col-md-8"> ?? ??? ??? ??? ?<form action="upload" method="post" enctype="multipart/form-data"> ?? ??? ??? ??? ??? ?<div class="form-group"> ?? ??? ??? ??? ??? ??? ?<div class="input-group col-md-4"> ?? ??? ??? ??? ??? ??? ??? ?<span class="input-group-addon"> ?? ??? ??? ??? ??? ??? ??? ??? ?<i class="glyphicon glyphicon-user"></i> ?? ??? ??? ??? ??? ??? ??? ?</span> ?? ??? ??? ??? ??? ??? ??? ?<input class="form-control" type="text" name="userName"> ?? ??? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ??? ?<div class="form-group"> ?? ??? ??? ??? ??? ??? ?<div class="input-group col-md-4"> ?? ??? ??? ??? ??? ??? ??? ?<span class="input-group-addon"> ?? ??? ??? ??? ??? ??? ??? ??? ?<i class="glyphicon glyphicon-search"></i> ?? ??? ??? ??? ??? ??? ??? ?</span> ?? ??? ??? ??? ??? ??? ??? ?<input class="form-control" type="file" name="head"> ?? ??? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ??? ?<div class="form-group"> ?? ??? ??? ??? ??? ??? ?<div class="col-md-4"> ?? ??? ??? ??? ??? ??? ??? ?<button type="submit" class="btn btn-success">注冊(cè)</button> ?? ??? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ??? ?</div> ?? ??? ??? ??? ?</form> ?? ??? ??? ?</div> ?? ??? ?</div> ?? ?</div> </body> </html>
程序清單:/springboot2/src/main/resources/templates/userInfo.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>用戶(hù)信息</title> <link rel="stylesheet" th:href="@{css/bootstrap.min.css}" > <link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" /> <script type="text/javascript" th:src="@{js/jquery-1.11.0.min.js}"></script> <script type="text/javascript" th:src="@{js/bootstrap.min.js}"></script> </head> <body> ?? ?<div class="panel panel-primary"> ?? ??? ?<div class="panel-heading"> ?? ??? ??? ?<h3 class="panel-title">Spring Boot文件下載</h3> ?? ??? ?</div> ?? ?</div> ?? ?<div class="container"> ?? ??? ?<div class="panel panel-primary"> ?? ??? ??? ?<div class="panel-heading"> ?? ??? ??? ??? ?<h3 class="panel-title">用戶(hù)信息列表</h3> ?? ??? ??? ?</div> ?? ??? ?</div> ?? ??? ?<div class="panel-body"> ?? ??? ??? ?<div class="table table-responsive"> ?? ??? ??? ??? ?<table class="table table-bordered" id="userTable"> ?? ??? ??? ??? ??? ?<tbody class="text-center"> ?? ??? ??? ??? ??? ??? ?<tr> ?? ??? ??? ??? ??? ??? ??? ?<td><img th:src="@{'upload/'+${user.head.originalFilename}}" height="30"></td> ?? ??? ??? ??? ??? ??? ??? ?<td th:text="${user.userName}">用戶(hù)名</td> ?? ??? ??? ??? ??? ??? ??? ?<td><a th:href="@{download(filename=${user.head.originalFilename })}" >下載</a></td> ?? ??? ??? ??? ??? ??? ?</tr> ?? ??? ??? ??? ??? ?</tbody> ?? ??? ??? ??? ?</table> ?? ??? ??? ?</div> ?? ??? ?</div> ?? ?</div> </body> </html>
程序清單:/springboot2/src/main/java/com/dwx/hello/User.java
package com.dwx.hello; import org.springframework.web.multipart.MultipartFile; public class User { ?? ?private String userName; ?? ?private MultipartFile head; ?? ?public String getUserName() { ?? ??? ?return userName; ?? ?} ?? ?public void setUserName(String userName) { ?? ??? ?this.userName = userName; ?? ?} ?? ?public MultipartFile getHead() { ?? ??? ?return head; ?? ?} ?? ?public void setHead(MultipartFile head) { ?? ??? ?this.head = head; ?? ?} }
程序清單:/springboot2/src/main/java/com/dwx/hello/UserController.java
package com.dwx.hello; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity.BodyBuilder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class UserController { ?? ?@RequestMapping("/registerForm") ?? ?public String registerForm() { ?? ??? ?return "register"; ?? ?} ?? ?@RequestMapping("/upload") ?? ?public String upload(HttpServletRequest request, ?? ??? ??? ?@ModelAttribute User user,Model model) throws IllegalStateException, IOException { ?? ??? ?if(!user.getHead().isEmpty()) { ?? ??? ??? ?String path=request.getServletContext().getRealPath("/upload"); ?? ??? ??? ?String fileName=user.getHead().getOriginalFilename(); ?? ??? ??? ?File filePath=new File(path,fileName); ?? ??? ??? ?if(!filePath.getParentFile().exists()) { ?? ??? ??? ??? ?filePath.getParentFile().mkdirs(); ?? ??? ??? ?} ?? ??? ??? ?user.getHead().transferTo(new File(path+File.separator+fileName)); ?? ??? ??? ?model.addAttribute("user", user); ?? ??? ??? ?return "userInfo"; ?? ??? ?}else { ?? ??? ??? ?return "error"; ?? ??? ?} ?? ?} ?? ?@RequestMapping(value="/download") ?? ? public ResponseEntity<byte[]> download(HttpServletRequest request, ?? ??? ??? ? @RequestParam("filename") String filename, ?? ??? ??? ? @RequestHeader("User-Agent") String userAgent, ?? ??? ??? ? Model model)throws Exception{ ?? ??? ?// 下載文件路徑 ?? ??? ?String path = request.getServletContext().getRealPath( ? ? ? ? ? ? ? ?"/upload/"); ?? ??? ?// 構(gòu)建File ?? ??? ?File file = new File(path+File.separator+ filename); ?? ??? ?// ok表示Http協(xié)議中的狀態(tài) 200 ? ? ? ?BodyBuilder builder = ResponseEntity.ok(); ? ? ? ?// 內(nèi)容長(zhǎng)度 ? ? ? ?builder.contentLength(file.length()); ? ? ? ?// application/octet-stream : 二進(jìn)制流數(shù)據(jù)(最常見(jiàn)的文件下載)。 ? ? ? ?builder.contentType(MediaType.APPLICATION_OCTET_STREAM); ? ? ? ?// 使用URLDecoder.decode對(duì)文件名進(jìn)行解碼 ? ? ? ?filename = URLEncoder.encode(filename, "UTF-8"); ? ? ? ?// 設(shè)置實(shí)際的響應(yīng)文件名,告訴瀏覽器文件要用于【下載】、【保存】attachment 以附件形式 ? ? ? ?// 不同的瀏覽器,處理方式不同,要根據(jù)瀏覽器版本進(jìn)行區(qū)別判斷 ? ? ? ?if (userAgent.indexOf("MSIE") > 0) { ? ? ? ? ? ? ? ?// 如果是IE,只需要用UTF-8字符集進(jìn)行URL編碼即可 ? ? ? ? ? ? ? ?builder.header("Content-Disposition", "attachment; filename=" + filename); ? ? ? ?} else { ? ? ? ? ? ? ? ?// 而FireFox、Chrome等瀏覽器,則需要說(shuō)明編碼的字符集 ? ? ? ? ? ? ? ?// 注意filename后面有個(gè)*號(hào),在UTF-8后面有兩個(gè)單引號(hào)! ? ? ? ? ? ? ? ?builder.header("Content-Disposition", "attachment; filename*=UTF-8''" + filename); ? ? ? ?} ? ? ? ?return builder.body(FileUtils.readFileToByteArray(file)); ?? ? } }
運(yùn)行Spring Boot項(xiàng)目,訪問(wèn)以下地址:http://localhost:8080/registerForm
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot中的文件上傳與下載詳解
- Java實(shí)現(xiàn)大文件的分片上傳與下載(springboot+vue3)
- SpringBoot文件上傳與下載功能實(shí)現(xiàn)詳解
- Axios+Spring?Boot實(shí)現(xiàn)文件上傳和下載
- SpringBoot上傳和下載文件的原理解析
- SpringBoot 文件或圖片上傳與下載功能的實(shí)現(xiàn)
- springboot+vue實(shí)現(xiàn)文件上傳下載
- 詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
- Spring Boot 文件上傳與下載的示例代碼
- SpringBoot 文件上傳和下載的實(shí)現(xiàn)源碼
- springboot 中文件上傳下載實(shí)例代碼
- SpringBoot實(shí)現(xiàn)文件上傳下載功能小結(jié)
- SpringBoot+ruoyi框架文件上傳和下載的實(shí)現(xiàn)
相關(guān)文章
SpringBoot動(dòng)態(tài)定時(shí)任務(wù)實(shí)現(xiàn)完整版
最近有幸要開(kāi)發(fā)個(gè)動(dòng)態(tài)定時(shí)任務(wù),這里簡(jiǎn)單再梳理一下,下面這篇文章主要給大家介紹了關(guān)于SpringBoot動(dòng)態(tài)定時(shí)任務(wù)實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02java數(shù)據(jù)類(lèi)型與變量的安全性介紹
這篇文章主要介紹了java數(shù)據(jù)類(lèi)型與變量的安全性介紹,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07JSON.toJSONString()空字段不忽略修改的問(wèn)題
這篇文章主要介紹了JSON.toJSONString()空字段不忽略修改的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot使用Sharding-JDBC實(shí)現(xiàn)數(shù)據(jù)分片和讀寫(xiě)分離的方法
本文主要介紹了SpringBoot使用Sharding-JDBC實(shí)現(xiàn)數(shù)據(jù)分片和讀寫(xiě)分離,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10一篇文章帶你了解mybatis的動(dòng)態(tài)SQL
這篇文章主要為大家介紹了mybatis的動(dòng)態(tài)SQL?,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01將字符串?dāng)?shù)字格式化為樣式1,000,000,000的方法
這篇文章主要介紹了將字符串?dāng)?shù)字格式化為樣式1,000,000,000的方法,有需要的朋友可以參考一下2014-01-01