前臺form 表單:設置method=post,enctype=multipart/form-data。
struts2在原有的上傳解析器繼承上做了進一步封裝,更進一步簡化了文件上傳。
action需要使用3個屬性來封裝該文件域的信息:
(1)類型為file的*屬性封裝了該文件域對應的文件內容;
(2)類型為string的***filename屬性封裝了該文件域對應的文件的文件類型;
(3)類型為string的***contenttype屬性封裝了該文件域對應的文件的類型。
具體實現:
新建web項目
添加struts2相關包
myeclipse可直接下載,右擊項目,如下。
前臺
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+ "://" +request.getservername()+ ":" +request.getserverport()+path+ "/" ; %> <html> <body> <form action= "upload.action" method= "post" enctype= "multipart/form-data" > <input type= "file" name= "upload" multiple= "multiple" /> <input type= "submit" value= "提交" /> </form> </body> </html> |
配置web.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version= "1.0" encoding= "utf-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= "3.0" > <display-name>uploadfile</display-name> <filter> <filter-name>struts2</filter-name> <filter- class >org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter- class > </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app> |
配置struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd" > <struts> <constant name= "struts.enable.dynamicmethodinvocation" value= "false" /> <constant name= "struts.devmode" value= "true" /> <constant name= "struts.multipart.savedir" value= "/tmp" /> <constant name= "struts.custom.i18n.resources" value= "app" ></constant> < package name= "default" namespace= "/" extends = "struts-default" > <action name= "upload" class = "com.yf.action.uploadaction" > <result>/index.jsp</result> <interceptor-ref name= "defaultstack" ></interceptor-ref> </action> </ package > </struts> |
后臺代碼
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
|
public class uploadaction extends actionsupport{ private list<file> upload; private list<string> uploadcontenttype; private list<string> uploadfilename; public list<file> getupload() { return upload; } public void setupload(list<file> upload) { this .upload = upload; } public list<string> getuploadcontenttype() { return uploadcontenttype; } public void setuploadcontenttype(list<string> uploadcontenttype) { this .uploadcontenttype = uploadcontenttype; } public list<string> getuploadfilename() { return uploadfilename; } public void setuploadfilename(list<string> uploadfilename) { this .uploadfilename = uploadfilename; } @override public string execute() throws exception { //文件保存路徑 string path = servletactioncontext.getservletcontext().getrealpath( "/images" ); file file = new file(path); //不存在則創建 if (!file.exists()){ file.mkdir(); } //循環將文件上傳到指定路徑 for ( int i = 0 ; i< upload.size(); i++){ fileutils.copyfile(upload.get(i), new file(file,uploadfilename.get(i))); } return success; } |
結果如下
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/giving_bestself/article/details/77513177