Struts2下使用Common-FileUpload实现文件上传

web应用下上传文件需要为表单设置enctype="multipart/form-data"属性,表单将以二进制编码的方式提交请求,然后由解析器进行解析,struts2不提供解析器,但可以和common-fileupload、COS很好的结合。struts2默认使用Jakarta的common-fileupload文件上传框架(在struts2-core.jar中default.properties中可见struts.multipart.parser=jakarta)。

下面展示一个使用common-fileupload实现文件上传的示例。

1、从http://commons.apache.org/上下载commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar,然后放到WEB-INF/lib文件夹下。

2、编写代码

upload-input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> 文件上传

标题:
文件:

upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 文件上传 标题:${title}
文件:${fileFileName }

UploadAction.java

 package com.petrochina.action.business; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.io.IOUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; private File file;// 对应文件域的file,封装文件内容 private String fileContentType;// 封装文件类型 private String fileFileName;// 封装文件名 private String savePath;// 保存路径 private String title;// 文件标题 @Override public String execute() throws Exception { if (file != null) { // System.out.println(getFileContentType()); // 读取文件内容到InputStream里 InputStream is = new FileInputStream(getFile()); // 创建输出流,生成新文件 OutputStream os = new FileOutputStream(getSavePath() + "//" + getFileFileName()); // 将InputStream里的byte拷贝到OutputStream IOUtils.copy(is, os); os.flush(); IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); return SUCCESS; } return INPUT; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getSavePath() { // 将相对路径转换成绝对路径 return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }

struts.xml

image/x-ms-bmp,image/jpeg,image/gif,image/png,image/x-png,application/excel,application/vnd.ms-excel 2048000 /test /WEB-INF/content/business/upload.jsp /WEB-INF/content/business/upload-input.jsp

 

效果如下:

上传页面:

上传成功:

项目结构图:

可见,成功在webapp/test/下上传了upload.png文件。

在struts.xml中配置fileUpload拦截器来限制上传文件的类型和大小时需要注意:

1、通过allowedTypes参数限制文件类型时允许类型定义要全,比如png类型的图片就有image/png和image/x-png两种类型,我开始只定义了image/png,发现png类型的图片上传不了,后来看了http://exceljava.javaeye.com/blog/210249发现我上传的图片类型是image/x-png,添加以后上传成功,所以遇到上传类型错误最好在本机输入看看是什么类型,然后在allowedTypes参数中添加。

2、通过maximunSize参数限制上传的文件大小时,如果没有改变常量“struts.multipart.maxSize”的值,那么maximunSize就不要设置超过2M,否则程序直接抛出异常,拦截器不会拦截,因为fileUpload拦截器默认使用“struts.multipart.maxSize”的值作为上传文件的最大值。当然,可以在struts.xml或者struts.properties中更改“struts.multipart.maxSize”的值,然后确保maximunSize参数的值不大于“struts.multipart.maxSize”的值。

3、对于拦截器的报错信息可以在国际化资源文件中增加

struts.messages.error.file.too.large=your error message

struts.messages.error.content.type.not.allowed=your error message

你可能感兴趣的:(Struts2,JAVA)