最近做的一個項目涉及到文件上傳與下載。前端上傳采用百度webUploader插件。有關該插件的使用方法還在研究中,日后整理再記錄。本文主要介紹SpringBoot后臺對文件上傳與下載的處理。
單文件上傳
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
// 單文件上傳 @RequestMapping (value = "/upload" ) @ResponseBody public 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即可。
多文件上傳
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//多文件上傳 @RequestMapping(value = "/uploadMore" , method = RequestMethod.POST) @ResponseBody public 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 "上傳成功" ; } |
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
//文件下載相關代碼 @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配置類對文件上傳進行全局控制。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Configuration public 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
總結
以上所述是小編給大家介紹的SpringBoot實現文件上傳下載功能小結,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/colton_null/article/details/76696674