Struts2上传文件通过配置文件实现文件过滤

来源:互联网 发布:淘宝排行榜如何设置 编辑:程序博客网 时间:2024/06/05 10:52
struts.xml实现过滤,使用拦截器
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><!-- 指定国际化资源文件的baseName为globalMessages --><constant name="struts.custom.i18n.resources"value="globalMessages"/><!-- 设置该应用使用的解码集 --><constant name="struts.i18n.encoding" value="UTF-8"/><package name="lee" extends="struts-default"><!-- 配置处理文件上传的Action --><action name="upload" class="lee.UploadAction"><!-- 配置fileUpload的拦截器 --><interceptor-ref name="fileUpload"><!-- 配置允许上传的文件类型 --><param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param><!-- 配置允许上传的文件大小 --><param name="maximumSize">2000000</param> </interceptor-ref> <!-- 配置系统默认的拦截器 --><interceptor-ref name="defaultStack"/><!-- 动态设置Action的属性值 <param name="savePath">/upload</param>--><!-- 配置input逻辑视图对应的视图页面 --><result name="input">/upload.jsp</result><!-- 配置Struts 2默认的视图页面 --><result>/succ.jsp</result></action></package></struts>

客户端端jsp请求
<%@ page contentType="text/html; charset=GBK" language="java"errorPage=""%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>使用List上传多个文件</title><meta name="website" content="http://www.crazyit.org" /></head><body><span style="color: red"><s:fielderror /></span><form action="upload.action" method="post"enctype="multipart/form-data">文件标题:<input type="text" name="title" /><br />选择第一个文件:<input type="file" name="upload" /><br />选择第二个文件:<input type="file" name="upload" /><br />选择第三个文件:<input type="file" name="upload" /><br /><input value="上传" type="submit" /></form></body></html>


Action处理

package lee;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.List;import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class UploadAction extends ActionSupport implements ServletContextAware {private String title;//使用File数组封装多个文件域对应的文件内容private List<File> upload;//使用字符串数组封装多个文件域对应的文件类型private List<String> uploadContentType;//使用字符串数组封装多个文件域对应的文件名字private List<String> uploadFileName;private ServletContext content;@Overridepublic String execute() throws Exception{//取得需要上传的文件数组List<File> files = getUpload();//遍历每个需要上传的文件for (int i = 0 ; i < files.size() ; i++){//以服务器的文件保存地址和原文件名建立上传文件输出流FileOutputStream fos = new FileOutputStream(content.getRealPath("/upload") + "\\" + getUploadFileName().get(i));//以每个需要上传的文件建立文件输入流FileInputStream fis = new FileInputStream(files.get(i));//将每个需要上传的文件写到服务器对应的文件中byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) > 0){fos.write(buffer , 0 , len);}fos.close();}return SUCCESS;    }//title属性的setter和getter方法public void setTitle(String title){this.title = title;}public String getTitle(){return this.title;}//upload属性的setter和getter方法public void setUpload(List<File> upload){this.upload = upload;}public List<File> getUpload(){return this.upload;}//uploadContentType属性的setter和getter方法public void setUploadContentType(List<String> uploadContentType){this.uploadContentType = uploadContentType;}public List<String> getUploadContentType(){return this.uploadContentType;}//uploadFileName属性的setter和getter方法public void setUploadFileName(List<String> uploadFileName){this.uploadFileName = uploadFileName;}public List<String> getUploadFileName(){return this.uploadFileName;}public ServletContext getContent() {return content;}public void setContent(ServletContext content) {this.content = content;}public void setServletContext(ServletContext content) {this.content = content;}}


配置程序中出错的键值

原创粉丝点击