Struts2控制文件上传与下载的几个注意事项:

(1)必须将表单的method设置为post,将enctype设置为multipart/from-data。只有这样,浏览器才会把用户选择文件的二进制数据发送给数据。

(2)Struts2默认使用的是Jakarta的Common-FileUpload的文件上传框架,因此,如果需要使用Struts2的文件上传功能,则需要在web应用中增加两个JAR文件,即commons-io-2.2.jar和commons-fileupload-1.3.1.jar。

一. 文件上传

(1)文件上传页面

   1: <form action="upload" enctype="multipart/form-data" method="post">

   2:     <input name="upload" type="file">

   3:     <input type="submit" value="submit">

   4: </form>

(2)处理上传请求的Action

   1: public class FileUploadAction extends ActionSupport{

   2:     //封装上传文件域

   3:     private File upload;

   4:  

   5:     //上传文件类型

   6:     private String uploadContentType;

   7:  

   8:     //上传文件名

   9:     private String uploadFileName;

  10:  

  11:     //上传文件的保存路径,在Struts.xml中配置

  12:     private String savePath;

  13:     

  14:     public File getUpload() {

  15:         return upload;

  16:     }

  17:     public void setUpload(File upload) {

  18:         this.upload = upload;

  19:     }

  20:     public String getUploadContentType() {

  21:         return uploadContentType;

  22:     }

  23:     public void setUploadContentType(String uploadContentType) {

  24:         this.uploadContentType = uploadContentType;

  25:     }

  26:     public String getUploadFileName() {

  27:         return uploadFileName;

  28:     }

  29:     public void setUploadFileName(String uploadFileName) {

  30:         this.uploadFileName = uploadFileName;

  31:     }

  32:     public String getSavePath() {

  33:         return ServletActionContext.getServletContext().getRealPath(savePath);

  34:     }

  35:     public void setSavePath(String savePath) {

  36:         this.savePath = savePath;

  37:     }

  38:     

  39:     public String execute() throws IOException

  40:     {

  41:         //获取文件的输出流

  42:         FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());

  43:  

  44:         //上传文件的输入流

  45:         FileInputStream fis = new FileInputStream(getUpload());

  46:         byte[] buffer = new byte[1024];

  47:         int len = 0;

  48:         while((len = fis.read(buffer)) > 0)

  49:         {

  50:             fos.write(buffer,0,len);

  51:         }

  52:         return SUCCESS;

  53:     }

  54: }

(3)利用拦截器实现文件过滤

通过配置拦截器,可以方便地控制上传文件的类型,大小。如果文件类型不符合,则会返回input视图。Struts2中文件上传的拦截器是fileUpload。

配置fileUpload拦截器时,可以为其指定两个参数:

  • allowedTypes: 该参数指定允许上传的文件类型

  • maximumSize: 该参数指定允许上传文件的大小,单位是字节。

另外,特别注意,为了使fileUpload拦截器生效,需要显示在其之后配置defaultStack的拦截器。

Struts.xml 配置

   1: <action name="upload" class="com.bigdata.action.FileUploadAction">

   2:     <interceptor-ref name="fileUpload">

   3:         <param name="allowedTypes">

   4:             application/octet-stream,application/zip,application/x-zip-compressed,

   5:             application/x-rar-compressed

   6:         </param>

   7:         <param name="maximumSize">20000000</param>

   8:     </interceptor-ref>

   9:     <interceptor-ref name="defaultStack" />

  10:     <param name="savePath">/data</param>

  11:     <result name="success">/jsp/index.jsp</result>

  12:     <result name="input">/uploadForm.jsp</result>

  13: </action>

二. 文件下载

Struts2的文件下载Action与普通的Action并没有什么太大的不同,仅仅是该Action需要提供一个返回InputStream流的方法,该输入流代表了被下载文件的入口,同时在配置文件中配置Action的result类型为stream。

Action类的实现:

   1: public class FileDownloadAction extends ActionSupport{

   2:     private String inputPath;

   3:     private String filename;

   4:     

   5:     public String getFilename() {

   6:         return filename;

   7:     }

   8:     public void setFilename(String filename) {

   9:         this.filename = filename;

  10:     }

  11:     public String getInputPath() {

  12:         return inputPath;

  13:     }

  14:     public void setInputPath(String inputPath){

  15:         this.inputPath = inputPath;

  16:     }

  17:     /*

  18:     定义一个返回InputStream的方法

  19:     该方法将被作为下载文件的入口,

  20:     且需要配置Stream类型结果时指定inputName参数

  21:     inputName参数的值就是该方法去掉get前缀,首字母小写的字符串

  22:     */

  23:     public InputStream getTargetFile() throws IOException{

  24:         String path = ServletActionContext.getServletContext().getRealPath(inputPath);

  25:         //下载文件名如果包含中文需要转码,避免出现乱码

  26:         String name = new String(filename.getBytes("ISO8859-1"),"UTF-8");

  27:         //下载文件的路径

  28:         String filePath = path + "\\" + name;

  29:         File file = new File(filePath);

  30:         return FileUtils.openInputStream(file);

  31:     }

  32:     

  33:     //设置下载文件的名称

  34:     public String getDownloadName(){

  35:         String downloadName = filename;

  36:         try {

  37:             downloadName = new String(downloadName.getBytes(),"ISO8859-1");

  38:         } catch (UnsupportedEncodingException e) {

  39:             // TODO Auto-generated catch block

  40:             e.printStackTrace();

  41:         }

  42:         return downloadName;

  43:     }

  44: }

Struts.xml的配置

配置下载文件的Action时,需要配置一个类型为stream的结果,需要指定如下4个属性。

  • contentType: 指定被下载文件的文件类型

  • inputName: 指定被下载文件的入口输入流
  • contentDisposition: 指定下载的文件名
  • bufferSize: 指定下载文件时的缓冲大小
   1: <action name="download" class="com.bigdata.action.FileDownloadAction">

   2:     <param name="inputPath">/data</param>

   3:     <result type="stream" name="success">

   4:         <param name="contentType">application/octet-stream</param>

   5:         <param name="inputName">targetFile</param>

   6:         <param name="contentDisposition">attachment;filename="${downloadName}"</param>

   7:     </result>

   8:     <result name="login">/jsp/login.jsp</result>

   9: </action>

三. 文件下载取消时出现异常

在struts2中使用result里type="stream"的结果类型时,当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常。

异常原因分析:

1. 配置其中result标签下的各个参数
2. 从服务器中获取输入流,并同时与客户端建立输出流(服务器与客户端链接通过Socket进行连接)
3. 当点击“保存”或“打开”时,开始传输数据。如果点击“取消”,关闭所有的流。这里要注意的是,但是实际发现Socket并没有断开!并且流也没有关闭!这一点非常重要!所以在JSP容器通过Response获取输出流之前,前面的流并没有关闭,所以会造成该异常的报出。

异常解决办法:

1. 下载插件struts2-sunspoter-stream-1.0.jar,并复制在/WEB-INF/lib下。

GitHub下载地址:https://github.com/41zone/StreamResultX

2. 在Struts.xml 中配置:

<result-types>

<result-type  name="streamx"     class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX"/>

</result-types>

3. 在下载的Action中配置result的类型为streamx:

   1: <action name="download" class="com.bigdata.action.FileDownloadAction">

   2:     <param name="inputPath">/data</param>

   3:     <result type="streamx" name="success">

   4:         <param name="contentType">application/octet-stream</param>

   5:         <param name="inputName">targetFile</param>

   6:         <param name="contentDisposition">attachment;filename="${downloadName}"</param>

   7:     </result>

   8:     <result name="login">/jsp/login.jsp</result>

   9: </action>

Struts2控制文件的上传与下载的更多相关文章

  1. 基于struts2框架文件的上传与下载

    在开发一些社交网站时,需要有允许用户上传自己本地文件的功能,则需要文件的上传下载代码. 首先考虑的是文件的储存位置,这里不考虑存在数据库,因为通过数据库查询获取十分消耗资源与时间,故需将数据存储在服务 ...

  2. 使用Struts2实现文件的上传和下载

    (一)单个文件的上传步骤: 1.拷贝jar包:commons-fileupload.jar,  commons-io.jar 下载链接(文件上传.rar):http://www.cnblogs.com ...

  3. struts2实现文件的上传和下载实例[转]

    实现原理 Struts 2是通过Commons FileUpload文件上传. Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器 ...

  4. Struts2中文件的上传与下载

    文件上传 1.jsp页面 <s:form action="fileAction" namespace="/file" method="POST& ...

  5. JSP文件的上传和下载

    文件上传下载,与传统的方式不同,这里能够上传和下载10G以上的文件.而且支持断点续传. 通常情况下,我们在网站上面下载的时候都是单个文件下载,但是在实际的业务场景中,我们经常会遇到客户需要批量下载的场 ...

  6. Struts2实现文件的上传与动态下载功能。

    本篇主要使用Struts2实现文件的上传与动态下载功能.出于安全考虑,所以,在硬盘上存储上传的文件的时候,统一都重新命名为随机字符串.用数据库存储真实文件名与随机文件名称之间的关联. 下面的是实体类 ...

  7. java实现文件的上传和下载

    1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...

  8. JavaWeb中文件的上传和下载

    JavaWeb中文件的上传和下载 转自: JavaWeb学习总结(五十)——文件上传和下载 - 孤傲苍狼 - 博客园https://www.cnblogs.com/xdp-gacl/p/4200090 ...

  9. Java中文件的上传与下载

    文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar  commons-io.jar apache的commons-fileupload实现文件上传,下载 [u ...

随机推荐

  1. Silverlight DataBinding Converter:根据binding对象调整显示

    Silverlight DataBinding Converter:根据binding对象调整显示 我希望写一系列关于Silverlight DataBinding的文章,分别讲解Silverligh ...

  2. 自定义android 音乐通知栏 ——可伸缩扩展

    Android custom notification for music player Example   In this tutorial, you will learn how to creat ...

  3. apache 安装及配置

    近期想用apache运行网站,在网上查询windows 版本的中文说明文档有特别少,所以将学习到的在这里做个笔记,以便日后学习以及大家相互交流. 相关文档:http://httpd.apache.or ...

  4. BZOJ_1713_[Usaco2007 China]The Bovine Accordion and Banjo Orchestra 音乐会_斜率优化

    BZOJ_1713_[Usaco2007 China]The Bovine Accordion and Banjo Orchestra 音乐会_斜率优化 Description Input 第1行输入 ...

  5. 聊聊Java SPI机制

    一.Java SPI机制 SPI(Service Provider Interface)是JDK内置的服务发现机制,用在不同模块间通过接口调用服务,避免对具体服务服务接口具体实现类的耦合.比如JDBC ...

  6. RobotFramework:App滑动屏幕

    转自:http://blog.csdn.net/jgw2008/article/details/77993399 在使用Robot Framework测试Android机器过程中, 经常要用到滚屏操作 ...

  7. ndoejs后台查询数据库返回的值-进行解析

    JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串

  8. python学习笔记3-循环1

    1 while break continue #while语句 ''' while 判断条件: 执行语句…… ''' count = 0 while (count < 9): print ('T ...

  9. hibernate的基础学习--一对多关联

    基本的用户和部门类,只有uuid和名称,没有其余字段. 配置文件 部门: <?xml version="1.0" encoding="utf-8" ?&g ...

  10. 基于puppeteer的网络拦截工具flyover

    为什么需要网络拦截工具 通常我们会遇到这样的场景: 线上一个图片有异常,线下修正之后需要发布之前进行测试,有些情况下,QA同学有直接在线上环境测试的需求,只是链接本地资源,通常在windows下有一个 ...