SpringBoot中的文件上傳與下載詳解
SpringBoot上傳與下載
前端頁(yè)面
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8"> <title>文件上傳</title> </head> <body> <form method="post" th:action="@{/monofileUpload}" enctype="multipart/form-data"> 單文件:<input type="file" name="monofile"> <input type="submit" value="提交"> </form> <form method="post" th:action="@{/multifileUpload}" enctype="multipart/form-data"> <!-- 要選擇多個(gè)文件需要寫(xiě) multiple --> 多文件:<input type="file" name="multifile" multiple> <input type="submit" value="提交"> </form> </body> </html>
文件上傳、下載
/** * 文件上傳、下載 */ @Controller public class FileTest { @GetMapping("/upload") public String upload() { return "file"; } /** * 單文件上傳 * MultipartFile 自動(dòng)封裝上傳過(guò)來(lái)的文件 * @param monofile 單個(gè)文件 * @return f/t */ @ResponseBody @PostMapping("/monofileUpload") public String upload(@RequestParam("monofile") MultipartFile monofile) throws IOException { // 判斷上傳文件是否為空,若為空則返回錯(cuò)誤信息 if (monofile.isEmpty()) { return "上傳失??!"; } // 獲取文件原名 String originalFilename = monofile.getOriginalFilename(); // 創(chuàng)建一個(gè)新的File對(duì)象用于存放上傳的文件 File fileNew = new File("D:\\Desktop\\" + originalFilename); monofile.transferTo(fileNew); return "上傳完成!"; } /** * 多文件上傳 * @param multifile 多個(gè)文件 * @return f/t */ @ResponseBody @PostMapping("/multifileUpload") public String upload(@RequestParam("multifile") MultipartFile[] multifile) throws IOException { if (multifile.length > 0) { for (MultipartFile file : multifile) { // 獲取文件原名 String originalFilename = file.getOriginalFilename(); // 創(chuàng)建一個(gè)新的File對(duì)象用于存放上傳的文件 File fileNew = new File("D:\\Desktop\\" + originalFilename); file.transferTo(fileNew); } return "上傳完成"; } return "上傳失?。?; } /** * 文件下載 * @param response */ @GetMapping("/download") public void download(HttpServletResponse response) { // 通過(guò)response輸出流將文件傳遞到瀏覽器 // 1、獲取文件路徑 String fileName = "test.png"; // 2.構(gòu)建一個(gè)文件通過(guò)Paths工具類(lèi)獲取一個(gè)Path對(duì)象 Path path = Paths.get("D:\\Desktop\\", fileName); //判斷文件是否存在 if (Files.exists(path)) { //存在則下載 //通過(guò)response設(shè)定他的響應(yīng)類(lèi)型 //4.獲取文件的后綴名 String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1); // 5.設(shè)置contentType ,只有指定contentType才能下載 response.setContentType("application/" + fileSuffix); // 6.添加http頭信息 // 因?yàn)閒ileName的編碼格式是UTF-8 但是http頭信息只識(shí)別 ISO8859-1 的編碼格式 // 因此要對(duì)fileName重新編碼 try { response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // 7.使用 Path 和response輸出流將文件輸出到瀏覽器 try { Files.copy(path, response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } } }
多文件上傳中遇到的問(wèn)題
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field fileName exceeds its maximum permitted size of 1048576 bytes.
Spring Boot默認(rèn)文件上傳大小為2M,多文檔上傳中總是出現(xiàn)文件大小超出限度
解決方法:
a、在application.properties文件中設(shè)置文件大小
//文件大小閾值,當(dāng)大于這個(gè)閾值時(shí)將寫(xiě)入到磁盤(pán),否則在內(nèi)存中。默認(rèn)值為0 spring.servlet.multipart.max-request-size=100MB //設(shè)置上傳文件總大小為100MB spring.servlet.multipart.max-file-size=100MB
b、在java文件中配置Bean來(lái)設(shè)置文件大小
/** * 文件上傳配置 * @return */ @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //單個(gè)文件最大 factory.setMaxFileSize("10240KB"); //KB,MB /// 設(shè)置總上傳數(shù)據(jù)總大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); }
到此這篇關(guān)于SpringBoot中的文件上傳與下載詳解的文章就介紹到這了,更多相關(guān)SpringBoot上傳與下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot文件上傳的原理解析
- SpringBoot+ruoyi框架文件上傳和下載的實(shí)現(xiàn)
- tdesign的文件上傳功能實(shí)現(xiàn)(微信小程序+idea的springboot)
- SpringBoot中的文件上傳和異常處理詳解
- Springboot文件上傳功能的實(shí)現(xiàn)
- SpringBoot文件上傳同時(shí)接收復(fù)雜參數(shù)的過(guò)程詳解
- SpringBoot實(shí)現(xiàn)項(xiàng)目文件上傳的方法詳解
- SpringBoot文件上傳與下載功能實(shí)現(xiàn)詳解
- SpringBoot簡(jiǎn)單實(shí)現(xiàn)文件上傳
- SpringBoot整合Hutool實(shí)現(xiàn)文件上傳的使用示例
相關(guān)文章
親手教你IDEA2020.3創(chuàng)建Javaweb項(xiàng)目的步驟詳解
這篇文章主要介紹了IDEA2020.3創(chuàng)建Javaweb項(xiàng)目的步驟詳解,本文是小編手把手教你,通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-03-03Springboot+Vue+axios實(shí)現(xiàn)文章收藏功能
這篇文章主要為大家詳細(xì)介紹了Springboot+Vue+axios實(shí)現(xiàn)文章收藏功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08SpringBoot獲取配置文件內(nèi)容的幾種方式總結(jié)
大家都知道SpringBoot獲取配置文件的方法有很多,下面這篇文章主要給大家介紹了關(guān)于SpringBoot獲取配置文件內(nèi)容的幾種方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02解析SpringBoot整合SpringDataRedis的過(guò)程
這篇文章主要介紹了SpringBoot整合SpringDataRedis的過(guò)程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06gateway、webflux、reactor-netty請(qǐng)求日志輸出方式
這篇文章主要介紹了gateway、webflux、reactor-netty請(qǐng)求日志輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot3整合EasyExcel動(dòng)態(tài)實(shí)現(xiàn)表頭重命名
這篇文章主要為大家詳細(xì)介紹了SpringBoot3整合EasyExcel如何通過(guò)WriteHandler動(dòng)態(tài)實(shí)現(xiàn)表頭重命名,文中的示例代碼講解詳細(xì),有需要的可以了解下2025-03-03java后臺(tái)調(diào)用HttpURLConnection類(lèi)模擬瀏覽器請(qǐng)求實(shí)例(可用于接口調(diào)用)
這篇文章主要介紹了java后臺(tái)調(diào)用HttpURLConnection類(lèi)模擬瀏覽器請(qǐng)求實(shí)例,該實(shí)例可用于接口調(diào)用,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10