Struts2 上传文件(2)

参考资料:

http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html

效果图:

WebContent/labratory/testFileUpload.jsp

1          <form action="${pageContext.request.contextPath}/upload2/upload2.do" 
2               enctype="multipart/form-data" method="post">
3             文件:<input type="file" name="image">
4                 <input type="submit" value="上传" />
5         </form>

src/struts.xml 第1部分

 1    
 2     
 3     
 4     <!-- http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html -->
 5     
 6     <!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
 7         如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
 8     <constant name="struts.action.extension" value="do" />
 9     
10     <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
11     <constant name="struts.serve.static.browserCache" value="false" />
12     
13     <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
14     <constant name="struts.configuration.xml.reload" value="true" />
15     
16     <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
17     <constant name="struts.devMode" value="true" />
18     
19     <!-- 默认的视图主题 -->
20     <constant name="struts.ui.theme" value="simple" />
21     <!--<constant name="struts.objectFactory" value="spring" />-->
22     
23     <!--解决乱码    -->
24     <constant name="struts.i18n.encoding" value="UTF-8" />
25     
26     <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) -->
27     <constant name="struts.multipart.maxSize" value="10701096"/>
28     
29     <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir -->
30     <constant name="struts.multipart.saveDir " value="d:/tmp" />

src/struts.xml 第2部分

 1     <!-- http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html 
 2         struts2之单个文件上传
 3     -->
 4     <package name="upload2" extends="struts-default">
 5     
 6         <action name="upload2" class="labratory.fileUpload.Action2" method="execute">
 7             <!-- 动态设置savePath的属性值 -->
 8             <!-- <param name="savePath">/images</param> -->
 9             <param name="savePath">/media</param>
10             <result name="success">/WEB-INF/page/message.jsp</result>
11             <result name="input">/upload/upload.jsp</result>
12             <interceptor-ref name="fileUpload">
13                 <!-- 文件过滤 -->
14                 <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
15                 
16                 <!-- http://blog.csdn.net/smcwwh/article/details/7349449 -->
17                 <!-- 允许后缀名为png,bmp,jpg,doc,xls的文件上传 -->     
18                 <!--
19                 <param name="allowedExtensions">  
20                     png,bmp,jpg,doc,xls
21                 </param> -->
22                 
23                 <!-- 文件大小, 以字节为单位 -->
24                 <param name="maximumSize">1025956</param>
25             </interceptor-ref>
26             <!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
27             <interceptor-ref name="defaultStack" />
28         </action>
29     </package>

src/labratory/fileUpload/Action2.java

  1 package labratory.fileUpload;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 
  8 import org.apache.struts2.ServletActionContext;
  9 
 10 import com.opensymphony.xwork2.ActionSupport;
 11 
 12 @SuppressWarnings("serial")
 13 public class Action2 extends ActionSupport {
 14 
 15     // 封装上传文件域的属性
 16     private File image;
 17     
 18     // 封装上传文件类型的属性
 19     private String imageContentType;
 20     
 21     // 封装上传文件名的属性
 22     private String imageFileName;
 23     
 24     // 接受依赖注入的属性
 25     private String savePath;
 26 
 27     @Override
 28     public String execute() {
 29         
 30         FileOutputStream fos = null ;
 31         FileInputStream fis = null ;
 32         
 33         try {
 34             
 35             System.out.println( getSavePath() );
 36             System.out.println( getImageFileName() );
 37 
 38             // 建立文件输出流
 39             fos = new FileOutputStream( getSavePath() + "\\" + getImageFileName());
 40             
 41             // 建立文件上传流
 42             fis = new FileInputStream( getImage() );
 43             byte[] buffer = new byte[1024];
 44             int len = 0 ;
 45             
 46             // 每次读 1024 字节
 47             while ( ( len = fis.read( buffer ) ) > 0 ) {
 48                 // 每次写 1024 字节
 49                 fos.write( buffer, 0, len ) ;
 50             }
 51             System.out.println("文件上传成功!");
 52             
 53         } catch ( Exception e ) {
 54             
 55             System.out.println("文件上传失败");
 56             e.printStackTrace();
 57             
 58         } finally {
 59             close( fos, fis ) ;
 60         }
 61         return SUCCESS;
 62     }
 63 
 64     /**
 65      * 返回上传文件的保存位置
 66      * 
 67      * @return
 68      */
 69     public String getSavePath() throws Exception{
 70         return ServletActionContext.getServletContext().getRealPath(savePath); 
 71     }
 72 
 73     public void setSavePath(String savePath) {
 74         this.savePath = savePath;
 75     }
 76 
 77     public File getImage() {
 78         return image;
 79     }
 80 
 81     public void setImage(File image) {
 82         this.image = image;
 83     }
 84 
 85     public String getImageContentType() {
 86         return imageContentType;
 87     }
 88 
 89     public void setImageContentType(String imageContentType) {
 90         this.imageContentType = imageContentType;
 91     }
 92 
 93     public String getImageFileName() {
 94         return imageFileName;
 95     }
 96 
 97     public void setImageFileName(String imageFileName) {
 98         this.imageFileName = imageFileName;
 99     }
100 
101     private void close(FileOutputStream fos, FileInputStream fis) {
102         if (fis != null) {
103             try {
104                 fis.close();
105             } catch (IOException e) {
106                 System.out.println("FileInputStream关闭失败");
107                 e.printStackTrace();
108             }
109         }
110         if (fos != null) {
111             try {
112                 fos.close();
113             } catch (IOException e) {
114                 System.out.println("FileOutputStream关闭失败");
115                 e.printStackTrace();
116             }
117         }
118     }
119 
120 }
原文地址:https://www.cnblogs.com/livon/p/3096846.html