SpringBoot下檔案上傳與下載的實現,springboot檔案上傳
本文歡迎轉載,轉載請註明出處,謝謝~(作者:喝酒不騎馬 Colton_Null)
from CSDN
SpringBoot後台如何?檔案上傳下載?
最近做的一個項目涉及到檔案上傳與下載。前端上傳採用百度webUploader外掛程式。有關該外掛程式的使用方法還在研究中,日後整理再記錄。本文主要介紹SpringBoot後台對檔案上傳與下載的處理。
單檔案上傳
// 單檔案上傳@RequestMapping(value = "/upload")@ResponseBodypublic String upload(@RequestParam("file") MultipartFile file) { try { if (file.isEmpty()) { return "檔案為空白"; } // 擷取檔案名稱 String fileName = file.getOriginalFilename(); logger.info("上傳的檔案名稱為:" + fileName); // 擷取檔案的尾碼名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); logger.info("檔案的尾碼名為:" + suffixName); // 設定檔案儲存體路徑 String filePath = "D://aim//"; String path = filePath + fileName + suffixName; File dest = new File(path); // 檢測是否存在目錄 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs();// 建立檔案夾 } file.transferTo(dest);// 檔案寫入 return "上傳成功"; } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "上傳失敗";}
如果想要修改檔案路徑及檔案名稱,修改filePath以及fileName即可。
多檔案上傳
//多檔案上傳@RequestMapping(value = "/uploadMore", method = RequestMethod.POST)@ResponseBodypublic String handleFileUpload(HttpServletRequest request) { List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file"); MultipartFile file = null; BufferedOutputStream stream = null; for (int i = 0; i < files.size(); ++i) { file = files.get(i); String filePath = "D://aim//"; if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); stream = new BufferedOutputStream(new FileOutputStream( new File(filePath + file.getOriginalFilename())));//設定檔案路徑及名字 stream.write(bytes);// 寫入 stream.close(); } catch (Exception e) { stream = null; return "第 " + i + " 個檔案上傳失敗 ==> " + e.getMessage(); } } else { return "第 " + i + " 個檔案上傳失敗因為檔案為空白"; } } return "上傳成功";}
檔案下載
//檔案下載相關代碼@RequestMapping("/download")public String downloadFile(HttpServletRequest request, HttpServletResponse response) { String fileName = "aim_test.txt";// 設定檔案名稱,根據業務需要替換成要下載的檔案名稱 if (fileName != null) { //設定檔案路徑 String realPath = "D://aim//" File file = new File(realPath , fileName); if (file.exists()) { response.setContentType("application/force-download");// 設定強制下載不開啟 response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 設定檔案名稱 byte[] buffer = new byte[1024]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); OutputStream os = response.getOutputStream(); int i = bis.read(buffer); while (i != -1) { os.write(buffer, 0, i); i = bis.read(buffer); } System.out.println("success"); } catch (Exception e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } return null;}
MultipartConfig配置
可以通過MultipartConfig配置類對檔案上傳進行全域控制。
@Configurationpublic class MultipartConfig { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // 置檔案大小限制 ,超出此大小頁面會拋出異常資訊 factory.setMaxFileSize("2MB"); //KB,MB // 設定總上傳資料總大小 factory.setMaxRequestSize("20MB"); // 設定檔案臨時檔案夾路徑 // factory.setLocation("E://test//"); // 如果檔案大於這個值,將以檔案的形式儲存,如果小於這個值檔案將儲存在記憶體中,預設為0 // factory.setMaxRequestSize(0); return factory.createMultipartConfig(); }}
注意事項
前後端檔案傳輸格式應為 multipart/form-data
著作權聲明:本文為博主原創文章,轉載前請聯絡作者,經允許後方可轉載。轉載後請註明出處,謝謝!>>喝酒不騎馬 http://blog.csdn.net/colton_null <<。郵箱mayuzhe1222@qq.com http://blog.csdn.net/Colton_Null/article/details/76696674