分享

Struts2下使用Common

 3d1901 2014-06-17

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./上下载commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar,然后放到WEB-INF/lib文件夹下。


2、编写代码


upload-input.jsp


  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="s" uri="/struts-tags" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>文件上传</title>  
  8. </head>  
  9. <body>  
  10. <s:fielderror></s:fielderror>  
  11. <form action="upload.action" method="post" enctype="multipart/form-data">  
  12. 标题:<input type="text" name="title">  
  13. <br/>  
  14. 文件:<input type="file" name="file">  
  15. <br>  
  16. <input type="submit" value="上传">  
  17. </form>  
  18. </body>  
  19. </html>  


upload.jsp


  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>文件上传</title>  
  8. </head>  
  9. <body>  
  10. 标题:${title}  
  11. <br/>  
  12. 文件:${fileFileName }  
  13. <br>  
  14. </body>  
  15. </html>  


UploadAction.java


 

  1. package com.petrochina.action.business;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8.   
  9. import org.apache.commons.io.IOUtils;  
  10. import org.apache.struts2.ServletActionContext;  
  11.   
  12. import com.opensymphony.xwork2.ActionSupport;  
  13.   
  14. public class UploadAction extends ActionSupport {  
  15.   
  16.     private static final long serialVersionUID = 1L;  
  17.     private File file;// 对应文件域的file,封装文件内容  
  18.     private String fileContentType;// 封装文件类型  
  19.     private String fileFileName;// 封装文件名  
  20.     private String savePath;// 保存路径  
  21.     private String title;// 文件标题  
  22.   
  23.     @Override  
  24.     public String execute() throws Exception {  
  25.         if (file != null) {  
  26.             // System.out.println(getFileContentType());  
  27.             // 读取文件内容到InputStream里  
  28.             InputStream is = new FileInputStream(getFile());  
  29.             // 创建输出流,生成新文件  
  30.             OutputStream os = new FileOutputStream(getSavePath() + "//" + getFileFileName());  
  31.             // 将InputStream里的byte拷贝到OutputStream  
  32.             IOUtils.copy(is, os);  
  33.             os.flush();  
  34.             IOUtils.closeQuietly(is);  
  35.             IOUtils.closeQuietly(os);  
  36.             return SUCCESS;  
  37.         }  
  38.         return INPUT;  
  39.     }  
  40.   
  41.     public File getFile() {  
  42.         return file;  
  43.     }  
  44.   
  45.     public void setFile(File file) {  
  46.         this.file = file;  
  47.     }  
  48.   
  49.     public String getFileContentType() {  
  50.         return fileContentType;  
  51.     }  
  52.   
  53.     public void setFileContentType(String fileContentType) {  
  54.         this.fileContentType = fileContentType;  
  55.     }  
  56.   
  57.     public String getFileFileName() {  
  58.         return fileFileName;  
  59.     }  
  60.   
  61.     public void setFileFileName(String fileFileName) {  
  62.         this.fileFileName = fileFileName;  
  63.     }  
  64.   
  65.     public String getSavePath() {  
  66.         // 将相对路径转换成绝对路径  
  67.         return ServletActionContext.getServletContext().getRealPath(savePath);  
  68.     }  
  69.   
  70.     public void setSavePath(String savePath) {  
  71.         this.savePath = savePath;  
  72.     }  
  73.   
  74.     public String getTitle() {  
  75.         return title;  
  76.     }  
  77.   
  78.     public void setTitle(String title) {  
  79.         this.title = title;  
  80.     }  
  81. }  


struts.xml


  1. <struts>  
  2.     <!-- struts常量定义 -->  
  3.       
  4.     <!-- 指定struts国际化资源文件的baseName -->  
  5.     <constant name="struts.custom.i18n.resources" value="messageResource"/>  
  6.     <!-- 指定每次HTTP请求时是否重新加载国际化资源文件,默认为false,开发阶段设为true -->  
  7.     <constant name="struts.i18n.reload" value="true"/>  
  8.     <!-- 配置应用的编码方式 -->  
  9.     <constant name="struts.i18n.encoding" value="UTF-8"/>  
  10.     <!-- struts.xml变更时是否自动重新加载 -->  
  11.     <constant name="struts.configuration.xml.reload" value="true"/>  
  12.     <!-- 限定上传文件最大大小4M,struts2默认maxSize为2M -->  
  13.     <constant name="struts.multipart.maxSize" value="4194304"></constant>  
  14.     <!-- 指定上传文件时临时文件的存放路径,设为"/tempUpload"将在 项目所在盘下创建文件夹tempUpload-->  
  15.     <constant name="struts.multipart.saveDir" value="/tempUpload"></constant>  
  16.       
  17.         <!-- action 定义 -->  
  18.     <package name="mystruts" extends="struts-default">  
  19.                 <action name="upload" class="com.petrochina.action.business.UploadAction">  
  20.             <!-- 配置fileUpload拦截器 -->  
  21.             <interceptor-ref name="fileUpload">  
  22.                 <!-- 配置允许上传的文件类型-->  
  23.                 <param name="allowedTypes">image/x-ms-bmp,image/jpeg,image/gif,image/png,image/x-png,application/excel,application/vnd.ms-excel</param>   
  24.                 <!-- 配置允许上传的文件大小 -->  
  25.                 <param name="maximumSize">2048000</param>  
  26.             </interceptor-ref>  
  27.             <interceptor-ref name="defaultStack"></interceptor-ref>  
  28.             <!-- 配置上传文件的保存的相对路径 -->  
  29.             <param name="savePath">/test</param>  
  30.             <!-- 配置逻辑视图和实际资源的对应关系 -->  
  31.             <result>/WEB-INF/content/business/upload.jsp</result>  
  32.             <result name="input">/WEB-INF/content/business/upload-input.jsp</result>  
  33.         </action>  
  34.     </package>  
  35. </struts>  


 


效果如下:


上传页面:


上传成功:


项目结构图:


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


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


1、通过allowedTypes参数限制文件类型时允许类型定义要全,比如png类型的图片就有image/png和image/x-png两种类型,我开始只定义了image/png,发现png类型的图片上传不了,后来看了http://exceljava./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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多