一、准备工作
表单准备:将 HTML表单 enctype属性设置为 multipart/form-data
表单需要使用 method为post的方式
添加<input type=“file”>字段
二、struts2对文件上传的支持
框架中,FileUpload拦截器和Jakarta Commons FileUpload组件可以完成文件的上传。
步骤:
1.在对应Action建立三个属性
private File [fileFieldName]; //这个fileFieldName对应页面上<s:file name=""/> 的name属性
private String [fileFieldName]ContentType;
private String [fileFieldName]FileName;
UploadAction.java
public class UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private File ppt;
private String pptContentType;
private String pptFileName;
private String pptDesc;
public String getPptDesc() {
return pptDesc;
}
public void setPptDesc(String pptDesc) {
this.pptDesc = pptDesc;
}
public File getPpt() {
return ppt;
}
public void setPpt(File ppt) {
this.ppt = ppt;
}
public String getPptContentType() {
return pptContentType;
}
public void setPptContentType(String pptContentType) {
this.pptContentType = pptContentType;
}
public String getPptFileName() {
return pptFileName;
}
public void setPptFileName(String pptFileName) {
this.pptFileName = pptFileName;
}
@Override
public String execute() throws Exception {
System.out.println(ppt);
System.out.println(pptContentType);
System.out.println(pptFileName);
System.out.println(pptDesc);
//使用IO流进行上传
ServletContext sac=ServletActionContext.getServletContext();
String dir=sac.getRealPath("/files/"+pptFileName);
System.out.println(dir);
FileOutputStream out =new FileOutputStream(dir);
FileInputStream in = new FileInputStream(ppt);
byte[] buffer=new byte[1024];
int len=0;
while((len=in.read(buffer))!=-1){
out.write(buffer,0,len);
}
out.close();
in.close();
return SUCCESS;
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="testUpload" method="post" enctype="multipart/form-data">
<s:file name="ppt" label="PPTFile">
</s:file>
<s:textfield name="pptDesc" label="PPTDesc" ></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
三、配置FileUploadInterceptor拦截器参数,限制上传,并给出提示
- maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this isnot related to the various properties found in struts.properties. Default to approximately 2MB.//单个文件上传最大值
- allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all types to be uploaded.//允许的上传文件类型,多个使用【,】分隔
- allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all extensions to be uploaded.//允许上传文件的扩展名,逗号分隔
可以在国际化资源中定义如下消息
- struts.messages.error.uploading - 文件上传出错
- struts.messages.error.file.too.large - 超过大小
- struts.messages.error.content.type.not.allowed - 文件内容类型不合法
- struts.messages.error.file.extension.not.allowed - 文件扩展名不合法
添加错误提示后的struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="i18n"></constant>
<package name="default" namespace="/" extends="struts-default">
<!-- 配置拦截器栈 -->
<interceptors>
<interceptor-stack name="hcx">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">2000</param>
<param name="fileUpload.allowedTypes">text/plain,text/xml</param>
<param name="fileUpload.allowedExtensions">txt,html</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 使用拦截器栈 -->
<default-interceptor-ref name="hcx"/>
<action name="testUpload" class="com.hcx.app.UploadAction">
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
但是目前提示消息不完善,具体可以参考org.apache.struts2下的struts-messages.properties文件,下面是例子中的国际化文件
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
注意: 在struts2的defaul.properties中有对上传文件
总的大小限制,为2M。
struts.multipart.maxSize=2097152。如果修改可以按照上述方式。