Struts2的文件上传及使用拦截器实现文件过滤

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6     
 7     <welcome-file-list>
 8         <welcome-file>index.jsp</welcome-file>
 9     </welcome-file-list>
10 
11     <filter>
12         <filter-name>struts2</filter-name>
13         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
14     </filter>
15 
16     <filter-mapping>
17         <filter-name>struts2</filter-name>
18         <url-pattern>/*</url-pattern>
19     </filter-mapping>
20     
21 </web-app>
View Code

index.jsp:

 1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="s" uri="/struts-tags" %>
 3 <html>
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 6 <title>Insert title here</title>
 7 </head>
 8 <body>
 9 <s:fielderror></s:fielderror>
10 <form action="upload" method="post" enctype="multipart/form-data">
11 <input type="file" name="file"><br>
12 <input type="submit" value="上传">
13 </form>
14 </body>
15 </html>
View Code

struts.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8     <constant name="struts.custom.i18n.resources" value="mess"></constant>
 9     <package name="default" extends="struts-default">
10         <action name="upload" class="com.action.FileUploadAction">
11             <interceptor-ref name="fileUpload">
12                 <!-- 只准许上传图片文件 -->
13                 <param name="allowedTypes">image/png,image/gif,image/jpeg</param>
14                 <!-- 图片最大不能超过50000字节 -->
15                 <param name="maximumSize">5000</param>
16             </interceptor-ref>
17             <interceptor-ref name="defaultStack"/>
18             <param name="savePath">/upload</param>
19             <result name="success">/WEB-INF/success.jsp</result>
20             <result name="input">/index.jsp</result>
21         </action>
22     </package>
23 
24 </struts>
View Code

FileUploadAction.java:

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileOutputStream;
 4 
 5 import org.apache.struts2.ServletActionContext;
 6 
 7 import com.opensymphony.xwork2.ActionSupport;
 8 
 9 /**
10  *@date 2014-4-18
11  */
12 public class FileUploadAction extends ActionSupport{
13 
14     private File file;
15     private String fileFileName;
16     private String fileContentType;
17     private String savePath;  //在struts.xml中配置的属性
18     public File getFile() {
19         return file;
20     }
21     public void setFile(File file) {
22         this.file = file;
23     }
24     public String getFileFileName() {
25         return fileFileName;
26     }
27     public void setFileFileName(String fileFileName) {
28         this.fileFileName = fileFileName;
29     }
30     public String getFileContentType() {
31         return fileContentType;
32     }
33     public void setFileContentType(String fileContentType) {
34         this.fileContentType = fileContentType;
35     }
36     public String getSavePath() {
37         return savePath;
38     }
39     public void setSavePath(String savePath) {
40         this.savePath = savePath;
41     }
42     
43     @Override
44     public String execute() throws Exception {
45         ServletActionContext.getRequest().setAttribute("filename",this.getFileFileName());
46         String webInfoPath=ServletActionContext.getServletContext().getRealPath("/WEB-INF/");
47         FileOutputStream fos=new FileOutputStream(webInfoPath+this.getSavePath()+File.separator+this.getFileFileName());
48         FileInputStream fis=new FileInputStream(this.getFile());
49         byte[] buffer=new byte[1024];
50         int len=0;
51         while((len=fis.read(buffer))>0){
52             fos.write(buffer,0,len);
53         }
54         return "success";
55     }
56     
57 }
View Code

success.jsp:

 1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 ${filename}上传成功...
 9 </body>
10 </html>
View Code

mess_zh_CN.properties:

1 struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u4E0D\u88AB\u5141\u8BB8
2 struts.messages.error.file.too.large=\u4E0A\u4F20\u7684\u6587\u4EF6\u4F53\u79EF\u8FC7\u5927
3 struts.messages.error.uploading=\u4E0A\u4F20\u6587\u4EF6\u8FC7\u7A0B\u4E2D\u51FA\u73B0\u672A\u77E5\u9519\u8BEF
View Code

struts.messages.error.content.type.not.allowed=上传的文件类型不被允许

struts.messages.error.file.too.large=上传的文件体积过大

struts.messages.error.uploading=上传文件过程中出现未知错误

posted on 2014-04-18 13:24  confirmBname  阅读(281)  评论(0编辑  收藏  举报

导航