利用Struts2 实现文件下载

简介:
原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dba10g.blog.51cto.com/764602/851613
 
需求如下:
进来项目中需要添加文件下载Excel功能;决定使用Struts2自带的文件下载功能
  1. 减轻工作量,提高工作效率,不需要再写常常的Header头了
  2. 要求不需要生成中间文件
Java Code
/* 
* $Id: FileDownloadAction.java 496318 2007-01-15 13:58:24Z husted $ 
* Licensed to the Apache Software Foundation (ASF) under one 
* or more contributor license agreements.    See the NOTICE file 
* distributed with this work for additional information 
* regarding copyright ownership.    The ASF licenses this file 
* to you under the Apache License, Version 2.0 (the 
* "License"); you may not use this file except in compliance 
* with the License.    You may obtain a copy of the License at 

*    http://www.apache.org/licenses/LICENSE-2.0 

* Unless required by applicable law or agreed to in writing, 
* software distributed under the License is distributed on an 
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
* KIND, either express or implied.    See the License for the 
* specific language governing permissions and limitations 
* under the License. 
*/
 
package org.apache.struts2.showcase.filedownload; 

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.InputStream; 

import org.apache.commons.lang.StringUtils; 
import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.struts2.ServletActionContext; 

import com.opensymphony.xwork2.Action; 

/** 
* Demonstrates file resource download. 
* Set filePath to the local file resource to download, 
* relative to the application root ("/images/struts.gif"). 

*/
 
public  class FileDownloadAction  implements Action { 
         private String inputPath; 
         private String fileName; 
         private String contentType; 
         /** 
         * 输入流 
         */
 
         private InputStream inputStream; 
         
         public  void setInputStream(InputStream inputStream) { 
     this.inputStream = inputStream; 
  } 

   public String getFileName() { 
     return fileName; 
  } 

   public  void setFileName(String fileName) { 
     this.fileName = fileName; 
  } 

   public String getContentType() { 
     return contentType; 
  } 

   public  void setContentType(String contentType) { 
     this.contentType = contentType; 
  } 

   public  void setInputPath(String value) { 
                inputPath = value; 
        } 

         /** 
         * 如果设置了inputStream 则将inputStream输出到浏览器 
         * 如果通过inputPath获取文件,并将文件输出至浏览器; 
         * @return 
         * @throws Exception 
         */
 
         public InputStream getInputStream()  throws Exception { 
           if(inputStream !=  null){ 
             return inputStream; 
          } 
                 return ServletActionContext.getServletContext().getResourceAsStream(inputPath); 
        } 

         public String execute()  throws Exception { 
           this.setContentType( "application/vnd.ms-excel"); 
           this.setFileName( "wk.xls"); 
           /** 
            * 生成Excls文件 
            */
 
           if(StringUtils.isBlank(inputPath)){ 
             ByteArrayOutputStream output =  new ByteArrayOutputStream();        
             HSSFWorkbook wb =  new HSSFWorkbook(); 
             HSSFSheet sheet = wb.createSheet( "new sheet 2"); 
                     // Create a row and put some cells in it. Rows are 0 based. 
                    HSSFRow row = sheet.createRow(0); 
                     // Create a cell and put a value in it. 
                    HSSFCell cell = row.createCell(0); 
                    cell.setCellValue(1); 
                     // Or do it on one line. 
                    row.createCell(1).setCellValue(1.2); 
                    row.createCell(2).setCellValue( "This is a string"); 
                    row.createCell(3).setCellValue( true);             
             wb.write(output); 
             InputStream is =  new ByteArrayInputStream(output.toByteArray()); 
              this.setInputStream(is); 
          } 
//            return is; 
                 return SUCCESS; 
        } 


 
Struts.xml
                 < action  name ="download2"  class ="org.apache.struts2.showcase.filedownload.FileDownloadAction" > 
                         < result  name ="success"  type ="stream" > 
                                 < param  name ="contentType" >${contentType} </ param > 
                                 < param  name ="inputName" >inputStream </ param > 
                                 < param  name ="contentDisposition" >filename=${fileName} </ param > 
                                 < param  name ="bufferSize" >4096 </ param > 
                         </ result > 
                 </ action >
 
遗留问题:中文名称; Struts 如果是图片等等 会默认打开。稍后完善

本文出自 “简单” 博客,请务必保留此出处http://dba10g.blog.51cto.com/764602/851613

目录
相关文章
swagger接口需要权限验证解决方案
当我们在使用swagger的情况下,经常会遇到需要授权或者请求带有token才可以访问接口,这里我们就是解决授权问题。
646 0
|
SQL 关系型数据库 MySQL
如何使用MySQL Binlog Digger 4.14对binlog日志进行挖掘分析以便快速恢复误删除数据
MySQL Binlog Digger是一款运行在windows操作系统的挖掘与分析MySQL binlog的可视化工具,通过它可以快速打回被误操作时的数据,例如:delete, insert, update操作,并依据这些误操作生成相应的undo回滚语句,以便快速恢复数据,此外,它还可以支持离线binlog挖掘分析与binlog下载,它仅支持dml操作的回滚,但不支持ddl的回滚。
5099 1
如何使用MySQL Binlog Digger 4.14对binlog日志进行挖掘分析以便快速恢复误删除数据
|
JSON 算法 fastjson
com.alibaba.fastjson转换JSONObject数据后顺序与原JSON字符串顺序不一致原因分析
Json字符串转JSONObject对象保证属性及其内部JSONObject有序(本身顺序而非需要指定排序)
3767 1
|
JSON NoSQL 安全
后端如何优雅地处理重复请求/并发请求?
后端如何优雅地处理重复请求/并发请求?
567 0
|
Python
python matplotlib绘制 3D图像专题 (三维柱状图、曲面图、散点图、曲线图合集)
python matplotlib绘制 3D图像专题 (三维柱状图、曲面图、散点图、曲线图合集)
2030 0
python matplotlib绘制 3D图像专题 (三维柱状图、曲面图、散点图、曲线图合集)
|
JavaScript 前端开发 程序员
好程序员web前端培训之JavaScript数组去重方法
  **好程序员**web前端培训之JavaScript数组去重方法,一周学习结束,老师布置我们要写一篇技术文章,脑袋里面的第一个想法就是数组去重,在网上也百度了一下,很多公司面试的时候有很大的几率会问数组去重的问题。所以今天我就给大家分享我所用过的四种数组去重的方法。
1659 0
|
前端开发 C#
WPF 实现拖动工具箱效果
原文:WPF 实现拖动工具箱效果     1.效果   点击左边的矩形拖动到右边canvas面板,右边面板添加矩形  2.布局  左边是个StockPanel,上面有个矩形,右边是个Canvas面板。
2196 0
|
JavaScript 前端开发
《jQuery Cookbook中文版》——1.3 用选择器和jQuery函数选择DOM元素
如果你在Web浏览器中运行这个HTML页面,就会看到这段代码执行一个浏览器alert()方法,告诉我们该页面包含6个元素。我首先选择所有的元素,然后用length属性返回jQuery包装器集中元素的数量,并将其传递给alert()方法。
1151 0