SpringMVC 中對文件上傳做了封裝,我們可以更加方便的實現文件上傳。從 Spring3.1
開始,對于文件上傳,提供了兩個處理器:
- CommonsMultipartResolver
- StandardServletMultipartResolver
第一個處理器兼容性較好,可以兼容 Servlet3.0 之前的版本,但是它依賴了commons-fileupload 這個第三方工具,所以如果使用這個,一定要添加 commons-fileupload 依賴。
第二個處理器兼容性較差,它適用于 Servlet3.0之后的版本,它不依賴第三方工具,使用它,可以直接做文件上傳。
CommonsMultipartResolver
添加依賴
1
2
3
4
5
|
< dependency > < groupId >commons-fileupload</ groupId > < artifactId >commons-fileupload</ artifactId > < version >1.4</ version > </ dependency > |
配置MultipartResolver
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/>
注意,這個 Bean 一定要有 id,并且 id 必須是 multipartResolver
創建jsp頁面
1
2
3
4
|
< form action = "/upload" method = "post" enctype = "multipart/form-data" > < input type = "file" name = "file" > < input type = "submit" value = "上傳" > </ form > |
注意文件上傳請求是 POST 請求,enctype 一定是 multipart/form-data
開發文件上傳接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@Controller public class FileUploadController { SimpleDateFormat sdf = new SimpleDateFormat( "/yyyy/MM/dd/" ); @RequestMapping ( "/upload" ) @ResponseBody public String upload(MultipartFile file, HttpServletRequest req) { String format = sdf.format( new Date()); String realPath = req.getServletContext().getRealPath( "/img" ) + format; File folder = new File(realPath); if (!folder.exists()) { folder.mkdirs(); } String oldName = file.getOriginalFilename(); String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf( "." )); try { file.transferTo( new File(folder, newName)); String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/img" + format + newName; return url; } catch (IOException e) { e.printStackTrace(); } return "failed" ; } } |
這個文件上傳方法中,一共做了四件事:
- 解決文件保存路徑,這里是保存在項目運行目錄下的 img 目錄下,然后利用日期繼續寧分類
- 處理文件名問題,使用 UUID 做新的文件名,用來代替舊的文件名,可以有效防止文件名沖突
- 保存文件
- 生成文件訪問路徑
這里還有一個小問題,在 SpringMVC 中,靜態資源默認都是被自動攔截的,無法訪問,意味著上傳成功的圖片無法訪問,因此,還需要我們在 SpringMVC 的配置文件中,再添加如下配置:
<mvc:resources mapping="/**" location="/"/>
完成之后,就可以訪問 jsp 頁面,做文件上傳了。
當然,默認的配置不一定滿足我們的需求,我們還可以自己手動配置文件上傳大小等:
1
2
3
4
5
6
7
8
9
10
11
12
|
< bean class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" id = "multipartResolver" > <!--默認的編碼--> < property name = "defaultEncoding" value = "UTF-8" /> <!--上傳的總文件大小--> < property name = "maxUploadSize" value = "1048576" /> <!--上傳的單個文件大小--> < property name = "maxUploadSizePerFile" value = "1048576" /> <!--內存中最大的數據量,超過這個數據量,數據就要開始往硬盤中寫了--> < property name = "maxInMemorySize" value = "4096" /> <!--臨時目錄,超過 maxInMemorySize 配置的大小后,數據開始往臨時目錄寫,等全部上傳完成后,再將數據合并到正式的文件上傳目錄--> < property name = "uploadTempDir" value = "file:///E:\\tmp" /> </ bean > |
StandardServletMultipartResolver
這種文件上傳方式,不需要依賴第三方 jar(主要是不需要添加 commons-fileupload 這個依賴),但是也不支持 Servlet3.0 之前的版本。
使用 StandardServletMultipartResolver ,那我們首先在 SpringMVC 的配置文件中,配置這個 Bean:
<bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver" id="multipartResolver"></bean>
注意,這里 Bean 的名字依然叫 multipartResolver
配置完成后,注意,這個 Bean 無法直接配置上傳文件大小等限制。需要在 web.xml 中進行配置(這里,即使不需要限制文件上傳大小,也需要在 web.xml 中配置 multipart-config):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< servlet > < servlet-name >springmvc</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:spring-servlet.xml</ param-value > </ init-param > < multipart-config > <!--文件保存的臨時目錄,這個目錄系統不會主動創建--> < location >E:\\temp</ location > <!--上傳的單個文件大小--> < max-file-size >1048576</ max-file-size > <!--上傳的總文件大小--> < max-request-size >1048576</ max-request-size > <!--這個就是內存中保存的文件最大大小--> < file-size-threshold >4096</ file-size-threshold > </ multipart-config > </ servlet > < servlet-mapping > < servlet-name >springmvc</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > |
配置完成后,就可以測試文件上傳了,測試方式和上面一樣。
多文件上傳
多文件上傳分為兩種,一種是 key 相同的文件,另一種是 key 不同的文件。
1 key 相同的文件
這種上傳,前端頁面一般如下:
1
2
3
4
|
< form action = "/upload2" method = "post" enctype = "multipart/form-data" > < input type = "file" name = "files" multiple> < input type = "submit" value = "上傳" > </ form > |
主要是 input 節點中多了 multiple 屬性。后端用一個數組來接收文件即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@RequestMapping ( "/upload2" ) @ResponseBody public void upload2(MultipartFile[] files, HttpServletRequest req) { String format = sdf.format( new Date()); String realPath = req.getServletContext().getRealPath( "/img" ) + format; File folder = new File(realPath); if (!folder.exists()) { folder.mkdirs(); } try { for (MultipartFile file : files) { String oldName = file.getOriginalFilename(); String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf( "." )); file.transferTo( new File(folder, newName)); String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/img" + format + newName; System.out.println(url); } } catch (IOException e) { e.printStackTrace(); } } |
2 key 不同的文件
key 不同的,一般前端定義如下:
1
2
3
4
5
|
< form action = "/upload3" method = "post" enctype = "multipart/form-data" > < input type = "file" name = "file1" > < input type = "file" name = "file2" > < input type = "submit" value = "上傳" > </ form > |
這種,在后端用不同的變量來接收就行了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@RequestMapping ( "/upload3" ) @ResponseBody public void upload3(MultipartFile file1, MultipartFile file2, HttpServletRequest req) { String format = sdf.format( new Date()); String realPath = req.getServletContext().getRealPath( "/img" ) + format; File folder = new File(realPath); if (!folder.exists()) { folder.mkdirs(); } try { String oldName = file1.getOriginalFilename(); String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf( "." )); file1.transferTo( new File(folder, newName)); String url1 = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/img" + format + newName; System.out.println(url1); String oldName2 = file2.getOriginalFilename(); String newName2 = UUID.randomUUID().toString() + oldName2.substring(oldName2.lastIndexOf( "." )); file2.transferTo( new File(folder, newName2)); String url2 = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/img" + format + newName2; System.out.println(url2); } catch (IOException e) { e.printStackTrace(); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/qiuwenli/p/13423986.html