Struts2文件上传

提供 FileUpload 拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容

fileUpload拦截器 默认在 defaultStack 栈中, 默认会执行的

在Action需要对上传文件内容进行接收

    页面:
        <input type="file" name="upload" />
    Action :
        public class UploadAction extends ActionSupport {
            // 接收上传内容
            // <input type="file" name="upload" />
            private File upload; // 这里变量名 和 页面表单元素 name 属性一致
            private String uploadContentType;
            private String uploadFileName;
        }
        * 格式 : 上传表单项name属性 + ContentType 、 上传表单项name属性 + FileName
        * 为三个对象 提供 setter 方法

通过FileUtils 提供 copyFile 进行文件复制,将上传文件 保存到服务器端

Struts2文件上传问题解决

配置 input 视图 ,作为上传出错后 跳转页面

在文件上传时,如果发生错误 ,fileUpload拦截器 会设置错误信息,workflow拦截器 跳转到 input 视图

struts.multipart.parser=jakarta 定义文件上传,采用 commons-fileupload 技术

* 同时支持 cos 、pell 上传技术 (如果使用其它上传技术,单独下载jar包 )

通过 struts.multipart.maxSize 常量设置文件上传总大小限制

* struts.multipart.maxSize=2097152 默认上传文件总大小 2MB

* 超过文件总大小,跳转input 视图, 通过 回显错误信息

在struts.xml 设置上传总大小

设置上传文件总大小,对所有上传form有效,只想对当前form进行设置,可以设置fileUpload拦截器属性

FileUpload 拦截器有 3 个属性可以设置.

* maximumSize: 上传文件的最大长度(以字节为单位), 默认值为 2 MB

* allowedTypes: 允许上传文件的类型, 各类型之间以逗号分隔

* allowedExtensions: 允许上传文件扩展名, 各扩展名之间以逗号分隔

如果针对fileUpload 进行参数设置,当出错时,在页面通过 回显错误信息

struts-messages.properties 文件里预定义 上传错误信息,通过覆盖对应key 显示中文信息

struts.messages.error.uploading=Error uploading: {0}

struts.messages.error.file.too.large=The file is to large to be uploaded: {0} “{1}” “{2}” {3}

struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} “{1}” “{2}” {3}

struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} “{1}” “{2}” {3}

修改为

struts.messages.error.uploading=上传错误: {0}

struts.messages.error.file.too.large=上传文件太大: {0} “{1}” “{2}” {3}

struts.messages.error.content.type.not.allowed=上传文件的类型不允许: {0} “{1}” “{2}” {3}

struts.messages.error.file.extension.not.allowed=上传文件的后缀名不允许: {0} “{1}” “{2}” {3}

多文件上传

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。

第二步:把form表的enctype设置为:“multipart/form-data“,如下:

<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
  <input  type="file" name="uploadImages">
  <input  type="file" name="uploadImages">
</form>

第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:

public class uploadAction{
  private File[] uploadImages;//得到上传的文件
  private String[] uploadImagesContentType;//得到文件的类型
  private String[] uploadImagesFileName;//得到文件的名称
  //这里略省了属性的getter/setter方法
  public String saveFiles() throws Exception{
        ServletContext sc = ServletActionContext.getServletContext();
        String realpath = sc.getRealPath("/uploadfile");
        try {
            if(uploadImages!=null&&uploadImages.length>0){
                 for(int i=0;i<uploadImages.length;i++){
                         File destFile = new File(realpath,uploadImageFileNames[i]);
                         FileUtils.copyFile(uploadImages[i], destFile);
                  }
             }
         } catch (IOException e) {
              e.printStackTrace();}return "success";}}

Struts2文件下载

1) struts2 完成文件下载,通过 结果集类型 (Result Type) stream 来完成的

struts-default.xml 定义

2) 使用Stream结果集 完成文件下载

文件下载原理: 服务器读取下载文件内容,通过Response响应流写回, 设置 ContentType、 ContentDisposition 头信息

public class StreamResult extends StrutsResultSupport {
    protected String contentType = "text/plain"; // contentType头信息  (下载文件对应 MIME协议规定类型 )
        * html --- text/html . txt--- text/plain
    protected String contentDisposition = "inline"; // ContentDisposition头信息 (下载文件打开方式 inline浏览器内部打开, attachment 以附件形式打开)

    protected String inputName = "inputStream";  // 需要Action中 提供 getInputStream 方法 返回 InputStream 提供下载文件 内容
}   

Action 提供 InputStream 返回值 getInputStream 方法 ——- 指定下载文件流

配置 stream 结果集 参数 {filename} —- 在Action 提供 getFilename

* 下载附件名乱码问题 , IE和火狐 解决不同

public String encodeDownloadFilename(String filename, String agent)
            throws IOException {
        if (agent.contains("Firefox")) { // 火狐浏览器
            filename = "=?UTF-8?B?"
                    + new BASE64Encoder().encode(filename.getBytes("utf-8"))
                    + "?=";
        } else { // IE及其他浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }

Struts2-文件上传下载的更多相关文章

  1. JAVA Web 之 struts2文件上传下载演示(二)(转)

    JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...

  2. JAVA Web 之 struts2文件上传下载演示(一)(转)

    JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...

  3. Struts2文件上传下载

    Struts2文件上传 Struts2提供 FileUpload拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容,fileUpload拦截器 默认在defau ...

  4. Struts2之文件上传下载

    本篇文章主要介绍如何利用struts2进行文件的上传及下载,同时给出我在编写同时所遇到的一些问题的解决方案. 文件上传 前端页面 <!-- 引入struts标签 --> <%@tag ...

  5. Struts2文件上传和下载(原理)

    转自:http://zhou568xiao.iteye.com/blog/220732 1.    文件上传的原理:表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:1)     ...

  6. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

  7. 【SSH2(实用文章)】--Struts2文件上传和下载的例子

    回想一下,再上一篇文章Struts2实现机制,该步骤做一步一步来解决,这种决心不仅要理清再次Struts2用法.映射机制及其在深入分析.最后一个例子来介绍Struts2一种用法,这里将做一个有关文件上 ...

  8. Struts2实现文件上传下载功能(批量上传)

    今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...

  9. 学习Struts--Chap07:Struts2文件上传和下载

    1.struts2文件上传 1.1.struts2文件上传的基本概述 在开发web应用的时候,我们一般会为用户提供文件上传的功能,比如用户上传一张图像作为头像等.为了能上传文件,我们必须将表单的met ...

  10. struts2 文件上传和下载,以及部分源代码解析

    struts2 文件上传 和部分源代码解析,以及一般上传原理 (1) 单文件上传 一.简单介绍 Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form ...

随机推荐

  1. pc端的企业网站(IT修真院test9)详解一个响应式完成的pc端项目

    一:引入bootstrap框架 昨天一直被bootstrap栅格系统折磨. why? 我本来想一边码字,一边学习栅格布局的.but不成功.这时我头脑已经昏了. 下午,我查看了bootstrap的官网, ...

  2. js实现日期显示的一些操作

    1.js获取当前日期(yyyy-mm-dd) 以下代码是获取到的当前日期: var myDate = new Date(); var year = myDate.getFullYear(); //获取 ...

  3. (转)Java线程:新特征-线程池

    Java线程:新特征-线程池   Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利 ...

  4. 关于前端的photoshop初探的学习笔记

    写在前边 这还是高三的时候暑假的时候学习这个软件时记的笔记呢,今天又在电脑上找到了它,总觉得不应该让他尘封在我的硬盘上,于是挂了出来.温馨提示:比较乱,写给自己看的,看不下去,按ctrl+W 笔记内容 ...

  5. JS遍历属性和方法

    引用原文:http://www.cnblogs.com/lishenglyx/archive/2008/12/08/1350573.html#undefined <script language ...

  6. eclipse中console的输出行数控制

    eclipse中console的输出行数控制 开发中,会遇到当输出大量的sql语句或者错误的时候,往往会因为console输出的限制而不能完整显示,所以我们自己就需要迫切的增加显示的行数,这样 就可以 ...

  7. Wamp之mysql密码故事

    注:有时候修改mysql密码会出现如下状况:密码改了,但新密码就是进不进去. 原因大概是语法错误.例如: >update user set password='hooray' where use ...

  8. 解决oracle数据库删除sql语句出现^H字样

    1:安装readline包 yum install readline* 2:安装源码包: rlwrap-0.30.tar.gz    ./configure && make & ...

  9. 使用rem来做响应式布局(js动态加载)

    <script> ;(function (doc,win) { var htmlEle=doc.documentElement; var reload="orientationc ...

  10. Python第一行代码

    Python版本:Python 3.6.1 0x01 命令行交互 在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果.现在,试试输入100+200,看看计 ...