码迷,mamicode.com
首页 > Web开发 > 详细

笔记:Struts2 文件上传和下载

时间:2017-01-27 15:52:42      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:on()   download   font   stack   调用   eth   byte   method   tty   

为了上传文件必须将表单的method设置为POST,将 enctype 设置为 muiltipart/form-data,只有设置为这种情况下,浏览器才会把用户选择文件的二进制数据发送给服务器。

  1. 上传解析器配置
    1. Struts2 没有提供自己的请求解析器,struts2 需要调用其他上传框架来解析二进制数据,struts2 默认使用 jakarta Common-FileUpload 的文件上传框架,需要在 Web 应用的 lib 中增加 commons-io-2.2.jar commons-fileupload-1.3.2.jar
    2. 通过 struts2 常量配置 struts.multipart.parser 来设置文件上传解析器,默认值 jakarta
    3. Struts.multipart.saveDir:上传文件的临时文件保存路径,默认值 javax.servlet.context.tempdir 配置的路径,该路径是Tomcat的安装路径下的 work\Catalina\localhost
    4. Struts.multipart.maxSize:整个表单请求内容的最大字节数,默认值 2097152
  2. 上传Action 解析
    1. 创建 java.io.File 类型的字段来接收上传的文件流;创建 xxxFileName xxxContentType 来读取原始上传的文件名称和文件类型,其中 xxx 表示的是 java.io.File 类型的字段名称,比如示例是名为 upload 字段接收上传的文件流,因此需要有 uploadFileName uploadContentType 来接收该文件流的原始文件名称和文件类型,示例代码如下:

      ? ?

      public class UploadAction extends ActionSupport {

      ? ?

      /**

      *

      */

      private static final long serialVersionUID = 283051583917637792L;

      ? ?

      private File upload;

      private String uploadFileName;

      private String uploadContentType;

      private String uploadPath;

      ? ?

      public UploadAction() {

      uploadPath = "upload";

      }

      ? ?

      @Override

      public String execute() throws Exception {

      ? ?

      if (upload == null) {

      addActionError("没有选择上传文件");

      return INPUT;

      }

      ? ?

      String filePath = ServletActionContext.getServletContext().getRealPath(uploadPath);

      ? ?

      java.io.File dir = new java.io.File(filePath);

      if (dir.exists() == false) {

      if (dir.mkdirs() == false) {

      addActionError("创建目录失败,目录路径=" + filePath);

      return INPUT;

      }

      }

      ? ?

      System.out.println("Upload FileName =" + uploadFileName);

      System.out.println("Upload ContentType =" + uploadContentType);

      ? ?

      FileOutputStream fileOutputStream = new FileOutputStream(filePath + File.pathSeparator + uploadFileName);

      FileInputStream fileInputStream = new FileInputStream(upload);

      byte[] buffer = new byte[4096];

      int len = 0;

      do {

      len = fileInputStream.read(buffer, 0, buffer.length);

      if (len > 0) {

      fileOutputStream.write(buffer, 0, len);

      }

      } while (len > 0);

      ? ?

      addActionMessage("上传完成,保存路径=" + filePath + File.pathSeparator + uploadFileName);

      ? ?

      return SUCCESS;

      }

      ? ?

      public File getUpload() {

      return upload;

      }

      ? ?

      public void setUpload(File upload) {

      this.upload = upload;

      }

      ? ?

      public String getUploadFileName() {

      return uploadFileName;

      }

      ? ?

      public void setUploadFileName(String uploadFileName) {

      this.uploadFileName = uploadFileName;

      }

      ? ?

      public String getUploadContentType() {

      return uploadContentType;

      }

      ? ?

      public void setUploadContentType(String uploadContentType) {

      this.uploadContentType = uploadContentType;

      }

      }

  3. fileUpload 拦截器
    1. fileUpload 拦截器是struts2 提供的一个文件上传拦截器,用于拦截不允许的上传文件类型和文件大小,需要配置二个参数来处理:
      1. 参数 allowedTypes:该参数指定允许上传的文件类型(contentType),多个文件类型以英文逗号隔开
      2. 参数 maximumSize:该参数指定允许上传的文件大小,单位字节
    2. 如果上传文件类型或者文件大小错误,fileUpload 拦截器将会读取全局国际化资源文件来进行提示,读取的Key如下:
      1. struts.messages.error.file.too.large:该key表示上传的文件太大的提示
        1. {0} 表示 ActionName
        2. {1} 表示上传原始文件名称
        3. {2} 表示服务器临时文件名称
        4. {3} 上传文件的 ContentType
      2. struts.messages.error.content.type.not.allowed:该key表示上传文件类型错误的提示
        1. {0} 表示 ActionName
        2. {1} 表示上传原始文件名称
        3. {2} 表示服务器临时文件名称
        4. {3} 上传文件的字节大小
        5. {4} 允许的上传文件字节大小
      3. struts.messages.error.uploading:该key表示上传文件出现一个未知错误的提示
    3. 配置示例代码

      <action name="upload" class="org.drsoft.actions.file.UploadAction">

      <interceptor-ref name="fileUpload">

      <param name="allowedTypes">image/png</param>

      <param name="maximumSize">102400</param>

      </interceptor-ref>

      <!--必须增加该项,如果不增加其他的拦截器会默认增加该拦截器-->

      <interceptor-ref name="defaultStack"/>

    ? ?

    <result name="success">/WEB-INF/content/file/upload.jsp</result>

    <result name="input">/WEB-INF/content/file/upload.jsp</result>

    </action>

  4. 下载文件 Action
    1. 下载文件的Action需要使用 stream 的结果类型,stream结果类型需要指定一个 inputName 参数,该参数指定了一个输入流,这个输入流是被下载文件的入口,详细参数如下:
      1. 参数 inputName:表示其Action类的返回 InputStream 的下载方法名称,示例,设置为 downloadFile 则会调用 Action类的 getDownloadFile 方法
      2. 参数 contentType:设置 httpResponse mime-type ,设置 response contentType 头,默认值 text/plain,通用值 application/octet-stream
      3. 参数 contentDisposition:设置响应报文头的 content disposition 值,用于指定浏览器的处理方式和下载文件名称,默认类型为 inline(内联),浏览器将会打开文件,可以设置 attachment(附件),浏览器将提示保存文件,参数示例:Inline;filename="downloadfile.zip",如果文件名称为中文,则需要使用 java.net.URLEncoder.encode(fileName, "UTF-8")
      4. 参数 contentLength:设置流的字节长度,用于浏览器的下载进度显示
      5. 参数 bufferSize:输入流和输出流的缓冲区字节大小
      6. 参数 allowCaching:如果设置为 false,会将 response 报文头的 Cache-Control设置为no-cache,默认值 true
      7. 参数 contentCharSet:设置内容编码,如果设置了值将会增加到x响应报文的 contentType 后面附加 ";charset=value"

        ? ?

    ? ?

? ?

笔记:Struts2 文件上传和下载

标签:on()   download   font   stack   调用   eth   byte   method   tty   

原文地址:http://www.cnblogs.com/li3807/p/6353860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!