利用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值