springmvc默認(rèn)的解析器里面是沒(méi)有加入對(duì)文件上傳的解析的,,使用springmvc對(duì)文件上傳的解析器來(lái)處理文件上傳的時(shí)需要用springmvc提供的multipartresolver的申明,又因?yàn)閏ommonsmultipartresolver實(shí)現(xiàn)了multipartresolver接口,所以我們可以在springmvc配置文件中這樣配置:
1
2
3
4
5
6
|
<bean id= "multipartresolver" class = "org.springframework.web.multipart.commons.commonsmultipartresolver" > <property name= "defaultencoding" value= "utf-8" /> <property name= "maxuploadsize" value= "10485760000" /> <property name= "maxinmemorysize" value= "40960" /> </bean> |
首先引入文件上傳所需要的包,commons-logging-*.jar commons-io-*.jar commons-fileupload-*.jar
新建一個(gè)jsp頁(yè)面.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding= "utf-8" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd" > <html> <head> <meta http-equiv= "content-type" content= "text/html; charset=utf-8" > <title>文件上傳</title> </head> <body> <%--<form action= "user/fileupload" method= "post" enctype= "multipart/form-data" >--%> <form action= "user/fileupload" method= "post" enctype= "multipart/form-data" > <input type= "file" name= "fileupload" /> <input type= "submit" value= "上傳" /> </form> </body> </html> |
springmvc上傳文件的形式有很多,這里我介紹兩種.
第一種,看controller
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
|
package gd.hz.springmvc.controller; import java.io.file; import java.io.ioexception; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.multipart.commons.commonsmultipartfile; import org.springframework.web.servlet.modelandview; @controller ( "usercontroller" ) @requestmapping ( "user" ) public class usercontroller { // 處理文件上傳一 @requestmapping (value = "fileupload" , method = requestmethod.post) public modelandview fileupload( @requestparam ( "fileupload" ) commonsmultipartfile file) { // 獲取文件類(lèi)型 system.out.println(file.getcontenttype()); // 獲取文件大小 system.out.println(file.getsize()); // 獲取文件名稱(chēng) system.out.println(file.getoriginalfilename()); // 判斷文件是否存在 if (!file.isempty()) { string path = "d:/" + file.getoriginalfilename(); file localfile = new file(path); try { file.transferto(localfile); } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } return new modelandview( "datasuccess" ); } } |
類(lèi)commonsmultipartfile為我們提供了許多對(duì)文件處理的方法.例如文件大小,上傳文件名稱(chēng),文件類(lèi)型,具體用法可以查看spring的文檔.transferto就是將文件輸出到指定地方.
文件上傳的第二種方法,這種方法比較常用:
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
47
48
49
|
package gd.hz.springmvc.controller; import java.io.file; import java.io.ioexception; import java.util.iterator; import javax.servlet.http.httpservletrequest; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.multipart.multipartfile; import org.springframework.web.multipart.multiparthttpservletrequest; import org.springframework.web.multipart.commons.commonsmultipartresolver; @controller ( "usercontroller" ) @requestmapping ( "user" ) public class usercontroller { // 處理文件上傳二 @requestmapping (value = "fileupload2" , method = requestmethod.post) public string fileupload2(httpservletrequest request) throws illegalstateexception, ioexception { // 設(shè)置上下方文 commonsmultipartresolver multipartresolver = new commonsmultipartresolver( request.getsession().getservletcontext()); // 檢查form是否有enctype="multipart/form-data" if (multipartresolver.ismultipart(request)) { multiparthttpservletrequest multirequest = (multiparthttpservletrequest) request; iterator<string> iter = multirequest.getfilenames(); while (iter.hasnext()) { // 由commonsmultipartfile繼承而來(lái),擁有上面的方法. multipartfile file = multirequest.getfile(iter.next()); if (file != null ) { string filename = "demoupload" + file.getoriginalfilename(); string path = "d:/" + filename; file localfile = new file(path); file.transferto(localfile); } } } return "datasuccess" ; } } |
multiparthttpservletrequest提供了更加靈活的方法,可以獲取多個(gè)文件和文件名,可以遍歷獲得每個(gè)文件.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://mylfd.iteye.com/blog/1893648