一、struts2上传单个文件
第一步:确保在WEB-INF/lib在commons-fileupload-x.x.x.jar、commons-io-x.x.x.jar两个jar文件。这两个文件可以从struts2下载文件中的lib中找到,或者到http://commons.apache.org/下载。
第二步:把form表的enctype设置为:"multipart/form-data",method设置成"post"如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file" name="uploadFile">
</form>
第三步:在Action类中添加以下属性,action类的代码如下所示:
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class TestAction {
private File uploadFile; // 得到上传的文件,此属性对应于表单中文件字段的名称
//下面的这两个属性的命名必须遵守上定的规则,即为"表单中文件字段的名称" + "相应的后缀"
private String uploadFileContentType; // 得到上传的文件的数据类型,
private String uploadFileFileName; // 得到上传的文件的名称
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public String execute() throws Exception {
String realPath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realPath);
if(uploadFile !=null ){
File destFile = new File(new File(realPath), uploadFileFileName);//根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
if(!destFile.getParentFile().exists())//判断路径"/images"是否存在
destFile.getParentFile().mkdirs();//如果不存在,则创建此路径
//将文件保存到硬盘上,因为action运行结束后,临时文件就会自动被删除
FileUtils.copyFile(uploadFile, destFile);
ActionContext.getContext().put("message", "文件上传成功!");
}
return "success";
}
}
form.jsp页面的代码如下所示:
<body>
<form action="${pageContext.request.contextPath }/demo/test.action" method="post" enctype="multipart/form-data">
文件:<input type="file" name="uploadFile"></input>
<input type="submit" name="submit" value="上传"></input>
</form>
</body>
代码${pageContext.request.contextPath }等于<%=request.getContextPath() %>。
result.jsp页面的代码如下:
<body>
文件类型:${uploadFileContentType }<br>
${uploadFileFileName} ${message}
</body>
本人 进行测试,发现此时只要上传的文件不要超过2M,都可以顺利上传成功。上传完成后会返回文件的文件名等信息。如下所示:
文件类型:application/octet-stream
desktop.ini 文件上传成功!
在应用程序部署的文件夹下会产生一个images文件夹,在此文件夹下会有一个desktop.ini文件。
同进在控制台打印了如下的信息,说明上传时产生的临时文件删除成功:
2010-11-12 14:52:59 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info
信息: Removing file uploadFile D:\workspace\eclipsespace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\struts_helloworld\upload__43a41dcc_12c3ec6e75a__8000_00000002.tmp
但要是我上传一个大于2M的文件,就会上传失败,控制台打印的信息如下所示:
警告: Unable to parse request
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3018061) exceeds the configured maximum (2097152)
。。。
警告: the request was rejected because its size (3018061) exceeds the configured maximum (2097152)
这是因为struts2一次上传文件时默认的总大小是2097152B,所以在默认情况下上传的文件不能大于2M。
此时在struts.xml文件中,增加代码<constant name="struts.multipart.maxSize" value="10701096"/> ,
将它的文件上传限制设为10M左右,上面的文件就能正常上传了。配置文件如下所示:
<struts> <constant name="struts.multipart.maxSize" value="10701096"/> <package name="myDemo" namespace="/demo" extends="struts-default"> <action name="test" class="demo.action.TestAction"> <result>/WEB-INF/jsp/result.jsp</result> </action> </package> </struts>
注意:struts2中的文件限制参数<constant name="struts.multipart.maxSize" value="10701096"/>并不是可以无限制的改大的,一般不能太大,以不超过10M的文件。这是因为web的性质决定的。基于web上传的文件都不能太大,如果要上传更大的文件,可以考虑使用浏览器插件(其实是一种通讯软件),类似于迅雷这样子的软件。这些软件都是基于socket来上传文件的。
二、struts2上传多文件
<1> 在form表单中的有多个file类型的input节点
<body>
<form action="${pageContext.request.contextPath }/demo/test.action" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="uploadFiles"></input>
文件2:<input type="file" name="uploadFiles"></input>
文件3:<input type="file" name="uploadFiles"></input>
<input type="submit" name="submit" value="上传"></input>
</form>
</body>
<2>action类如下所示:
public class TestAction {
// 可以定义成数组类型,也可以定义成list
private File[] uploadFiles; // 得到上传的文件,此属性对应于表单中文件字段的名称
// 下面的这两个属性的命名必须遵守上定的规则,即为"表单中文件字段的名称" + "相应的后缀"
private String[] uploadFilesContentType; // 得到上传的文件的数据类型,
private String[] uploadFilesFileName; // 得到上传的文件的名称
//setter和getter方法
public String execute() throws Exception {
String realPath = ServletActionContext.getServletContext().getRealPath(
"/images");
File file = new File(realPath);
if (!file.exists())
file.mkdirs();
if (uploadFiles != null) {
for (int i = 0; i < uploadFiles.length; i++) {
File uploadFile = uploadFiles[i];
FileUtils.copyFile(uploadFile, new File(file,
uploadFilesFileName[i]));
}
ActionContext.getContext().put("message", "文件上传成功!");
}
return "success";
}
}