Struts2默认采用Jakarta的Common-FileUpload文件上传框架,因此我们需要在应用中添加两个jar包:commons-io-1.4.jar和commons-fileupload-1.2.1.jar。
假设文件上传页面为:
<s:form action="upload.action" method="post" enctype="multipart/form-data">
<s:textfield label="文件标题" name="title"/>
<s:file label="选择文件" name="upload"></s:file>
<s:submit value="上传"/>
</s:form>
在Action中,我们需要3个属性来封装文件域的信息:
- 类型为File的xxx属性,封装该文件域对应的文件内容。
- 类型为String的xxxFileName属性,封装文件域对应文件的文件名。
- 类型为String的xxxContentType属性,封装了该文件域对应文件的文件类型。
UploadAction代码如下:
package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
//封装文件标题请求参数的属性
private String title;
//封装上传文件域的属性
private File upload;
//封装上传文件类型的属性
private String uploadContentType;
//封装上传文件名的属性
private String uploadFileName;
//接受依赖注入的属性,文件保存路径
private String savePath;
//允许上传的文件类型
// private String allowTypes;
@Override
public String execute() throws Exception {
//先判断文件上传类型
// String filterResult = filterType(getAllowTypes().split(","));
// if(filterResult != null){
// ActionContext.getContext().put("typeError","您要上传的文件类型不正确");
// return filterResult; //filterResult此时等于input
// }
if(getUpload() == null) {
ActionContext.getContext().put("typeError","您要上传的文件类型不正确");
return INPUT;
}
//以服务器的文件保存地址savePath和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
//以上传文件建立一个文件上传流
FileInputStream fis = new FileInputStream(getUpload());
//将上传文件的内容写入服务器
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) > 0) {
fos.write(buffer,0,len);
}
// TODO Auto-generated method stub
return SUCCESS;
}
// public String getAllowTypes() {
// return allowTypes;
// }
//
// public void setAllowTypes(String allowTypes) {
// this.allowTypes = allowTypes;
// }
public String getSavePath() throws Exception {
//获取上传路径
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
/**
* 过滤文件类型
* @param types 系统所有允许上传的文件类型
* @return 如果上传文件的类型符合要求,返回null,否则返回input字符串
*/
public String filterType(String[] types) {
//得到上传文件的类型
String fileType = getUploadContentType();
//遍历允许上传的文件类型,并判断是否允许上传
for(String type : types) {
if(type.equals(fileType))
return null;
}
return INPUT;
}
}
然后在struts.xml文件中进行配置:
<action name="upload" class="com.test.action.UploadAction"> <!-- 配置fileUpload的拦截器 --> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param> <param name="maximumSize">102400</param> </interceptor-ref> <!-- 如果需要使用文件上传拦截器来过滤文件大小或者过滤文件内容,则必须显式配置引用Struts默认拦截器栈 --> <interceptor-ref name="defaultStack"/> <param name="savePath">/upload</param> <!-- <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param> --> <result>success.jsp</result> <result name="input">upload.jsp</result> </action>