前台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