SpringBoot中的文件上傳與下載詳解
SpringBoot上傳與下載
前端頁面
<!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"> <!-- 要選擇多個文件需要寫 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 自動封裝上傳過來的文件 * @param monofile 單個文件 * @return f/t */ @ResponseBody @PostMapping("/monofileUpload") public String upload(@RequestParam("monofile") MultipartFile monofile) throws IOException { // 判斷上傳文件是否為空,若為空則返回錯誤信息 if (monofile.isEmpty()) { return "上傳失敗!"; } // 獲取文件原名 String originalFilename = monofile.getOriginalFilename(); // 創(chuàng)建一個新的File對象用于存放上傳的文件 File fileNew = new File("D:\\Desktop\\" + originalFilename); monofile.transferTo(fileNew); return "上傳完成!"; } /** * 多文件上傳 * @param multifile 多個文件 * @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)建一個新的File對象用于存放上傳的文件 File fileNew = new File("D:\\Desktop\\" + originalFilename); file.transferTo(fileNew); } return "上傳完成"; } return "上傳失?。?; } /** * 文件下載 * @param response */ @GetMapping("/download") public void download(HttpServletResponse response) { // 通過response輸出流將文件傳遞到瀏覽器 // 1、獲取文件路徑 String fileName = "test.png"; // 2.構建一個文件通過Paths工具類獲取一個Path對象 Path path = Paths.get("D:\\Desktop\\", fileName); //判斷文件是否存在 if (Files.exists(path)) { //存在則下載 //通過response設定他的響應類型 //4.獲取文件的后綴名 String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1); // 5.設置contentType ,只有指定contentType才能下載 response.setContentType("application/" + fileSuffix); // 6.添加http頭信息 // 因為fileName的編碼格式是UTF-8 但是http頭信息只識別 ISO8859-1 的編碼格式 // 因此要對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(); } } } }
多文件上傳中遇到的問題
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field fileName exceeds its maximum permitted size of 1048576 bytes.
Spring Boot默認文件上傳大小為2M,多文檔上傳中總是出現文件大小超出限度
解決方法:
a、在application.properties文件中設置文件大小
//文件大小閾值,當大于這個閾值時將寫入到磁盤,否則在內存中。默認值為0 spring.servlet.multipart.max-request-size=100MB //設置上傳文件總大小為100MB spring.servlet.multipart.max-file-size=100MB
b、在java文件中配置Bean來設置文件大小
/** * 文件上傳配置 * @return */ @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //單個文件最大 factory.setMaxFileSize("10240KB"); //KB,MB /// 設置總上傳數據總大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); }
到此這篇關于SpringBoot中的文件上傳與下載詳解的文章就介紹到這了,更多相關SpringBoot上傳與下載內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
親手教你IDEA2020.3創(chuàng)建Javaweb項目的步驟詳解
這篇文章主要介紹了IDEA2020.3創(chuàng)建Javaweb項目的步驟詳解,本文是小編手把手教你,通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-03-03解析SpringBoot整合SpringDataRedis的過程
這篇文章主要介紹了SpringBoot整合SpringDataRedis的過程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06gateway、webflux、reactor-netty請求日志輸出方式
這篇文章主要介紹了gateway、webflux、reactor-netty請求日志輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot3整合EasyExcel動態(tài)實現表頭重命名
這篇文章主要為大家詳細介紹了SpringBoot3整合EasyExcel如何通過WriteHandler動態(tài)實現表頭重命名,文中的示例代碼講解詳細,有需要的可以了解下2025-03-03java后臺調用HttpURLConnection類模擬瀏覽器請求實例(可用于接口調用)
這篇文章主要介紹了java后臺調用HttpURLConnection類模擬瀏覽器請求實例,該實例可用于接口調用,具有一定的實用價值,需要的朋友可以參考下2014-10-10