1、单文件上传
页面表单MIME封装类型为enctype="multipart/form-data" ,提交方式为post,Struts2使用FileUpload实现文件上传
jsp
<form action="${pageContext.request.contextPath}/demo01/upload1.action" method="post" enctype="multipart/form-data">
<input type="file" name="upload"><br/>
<input type="submit">
</form>
action,Action中必须定义三个变量,变量名称和jsp表单中的name属性对应,File类型的“xxx”、String类型的“xxxContentType”、String类型的“xxxFileName”
public class UploadAction1 extends ActionSupport {
private File upload;
private String uploadContentType;
private String uploadFileName;
@Override
public String execute() throws Exception {
File destFile = new File(ServletActionContext.getServletContext().getRealPath("/upload") + "/" + uploadFileName);
FileUtils.copyFile(upload, destFile);
return NONE;
}
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
配置
<package name="demo01" namespace="/demo01" extends="struts-default"> <action name="upload1" class="demo01.UploadAction1"></action> </package>
2、文件上传的常量配置
通过fileUpload拦截器设置文件上传类型和大小,设置只对当前action有效
<package name="demo01" namespace="/demo01" extends="struts-default"> <action name="upload1" class="demo01.UploadAction1"> <result name="input">/demo01/upload1.jsp</result> <interceptor-ref name="defaultStack"> <param name="fileUpload.allowedExtensions">.mp3,.txt</param> <param name="fileUpload.maximumSize">6000000</param> </interceptor-ref> </action> </package>
在src目录下配置国际化资源文件messages.properties
struts.messages.error.content.type.not.allowed = \u6587\u4EF6\u7C7B\u578B\u9519\u8BEF struts.messages.error.uploading = \u4E0A\u4F20\u51FA\u73B0\u9519\u8BEF #struts.messages.error.file.too.large = \u6587\u4EF6\u592A\u5927 struts.messages.error.file.too.large =\u4E0A\u4F20\u6587\u4EF6{1}\u8D85\u8FC7\u5141\u8BB8\u5927\u5C0F{3} struts.messages.error.file.extension.not.allowed=\u4E0A\u4F20\u6587\u4EF6\u540D\u6269\u5C55\u540D\u4E0D\u7B26\u5408\u8981\u6C42
struts.xml中注册使用
<constant name="struts.custom.i18n.resources" value="messages"></constant>
这种全局资源文件配置没有任何问题,但我在action的所在包中配置国际化文件却失败了
页面需要fielderror显示
<s:fielderror></s:fielderror>
<form action="${pageContext.request.contextPath}/demo01/upload1.action" method="post" enctype="multipart/form-data">
<input type="file" name="upload"><br/>
<input type="submit">
</form>
也可以在struts.xml中配置全局常量,但这错误信息需要actionerror显示
<constant name="struts.multipart.maxSize" value="200000000"></constant>
3、多文件上传
jsp,上传文件的name属性一样
<form action="${pageContext.request.contextPath}/demo01/upload2.action" method="post" enctype="multipart/form-data">
<input type="file" name="upload"><br/>
<input type="file" name="upload"><br/>
<input type="file" name="upload"><br/>
<input type="submit">
</form>
Action,在Action中使用数组接受
public class UploadAction2 extends ActionSupport {
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
@Override
public String execute() throws Exception {
for (int i = 0; upload != null && i < upload.length; i++) {
File sourFile = upload[i];
File destFile = new File(ServletActionContext.getServletContext().getRealPath("/upload") + "/" + uploadFileName[i]);
FileUtils.copyFile(sourFile, destFile);
}
return NONE;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
struts配置
<action name="upload2" class="demo01.UploadAction2"> <result name="input">/demo01/upload2.jsp</result> </action>
所有的上传在操作之前需要判断是否有文件上传,防止空文件上传