利用Struts2 实现文件下载

简介:
需求如下:
进来项目中需要添加文件下载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 如果是图片等等 会默认打开。稍后完善



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/851613,如需转载请自行联系原作者
相关文章
|
缓存 前端开发 数据安全/隐私保护
【Flutter 前端技术开发专栏】Flutter 中的键盘处理与输入框优化
【4月更文挑战第30天】本文探讨了Flutter中键盘处理与输入框优化的关键技术,包括监听键盘显示隐藏、焦点管理、键盘类型适配、输入框高度自适应、处理键盘遮挡问题及性能优化。通过使用WidgetsBindingObserver、FocusNode和TextInputType等工具,开发者能提升用户体验,确保输入框在各种场景下的良好表现。实例分析和实践建议有助于开发者将这些方法应用于实际项目。
807 0
【Flutter 前端技术开发专栏】Flutter 中的键盘处理与输入框优化
|
设计模式 关系型数据库 Java
顺畅的职责传递-用责任链模式优化决策流程
本文首先通过经典场景展示了不使用设计模式时的问题与痛点。接着,引入责任链模式,详细讲解了其定义、解决问题的方式、结构图及工作原理,并通过重构示例展示了该模式如何解决原有痛点。最后,对责任链模式的优势、缺点以及在实际应用中可能遇到的挑战和限制进行了总结。责任链模式通过解耦请求发送者和接收者,提供了灵活的请求处理机制,适用于多个处理者按顺序处理请求的场景。然而,该模式也可能导致请求得不到处理或性能下降等问题,需在实际应用中权衡利弊。
871 0
顺畅的职责传递-用责任链模式优化决策流程
|
Web App开发 算法
软件破解初级实例教程(附工具附图)
最近在群里总是看到很多新朋友在问: 1、“新手怎么学破解啊?”(这是标准的伸手党,baidu google其实很好用) 2、“哎呀XX大牛,我什么基础都没有啊我不会汇编,不会C更不会C++还不会…………总之高手会的我都不会,我能学么?”(明确的告诉你,你能!你不和唐僧一样罗嗦的话你一定能。
12313 1
【UI】 修改element-ui input输入框placeholder提示信息、占位符的样式
【UI】 修改element-ui input输入框placeholder提示信息、占位符的样式
1755 0
|
SQL 消息中间件 分布式计算
菜鸟供应链实时数仓的架构演进及应用场景
菜鸟数据&规划部高级数据技术专家贾元乔从数据模型、数据计算、数据服务等几个方面介绍了菜鸟供应链数据团队在实时数据技术架构上的演进,以及在供应链场景中典型的实时应用场景和 Flink 的实现方案。
菜鸟供应链实时数仓的架构演进及应用场景
|
物联网 智能硬件
IOE与IOT:有什么区别?
互联网的各种用途层出不穷,其中许多已经在我们的智能家居中变得越来越常见。
1467 0
IOE与IOT:有什么区别?
|
7天前
|
人工智能 安全 Linux
【OpenClaw保姆级图文教程】阿里云/本地部署集成模型Ollama/Qwen3.5/百炼 API 步骤流程及避坑指南
2026年,AI代理工具的部署逻辑已从“单一云端依赖”转向“云端+本地双轨模式”。OpenClaw(曾用名Clawdbot)作为开源AI代理框架,既支持对接阿里云百炼等云端免费API,也能通过Ollama部署本地大模型,完美解决两类核心需求:一是担心云端API泄露核心数据的隐私安全诉求;二是频繁调用导致token消耗过高的成本控制需求。
4803 7
|
14天前
|
人工智能 JavaScript Ubuntu
5分钟上手龙虾AI!OpenClaw部署(阿里云+本地)+ 免费多模型配置保姆级教程(MiniMax、Claude、阿里云百炼)
OpenClaw(昵称“龙虾AI”)作为2026年热门的开源个人AI助手,由PSPDFKit创始人Peter Steinberger开发,核心优势在于“真正执行任务”——不仅能聊天互动,还能自动处理邮件、管理日程、订机票、写代码等,且所有数据本地处理,隐私完全可控。它支持接入MiniMax、Claude、GPT等多类大模型,兼容微信、Telegram、飞书等主流聊天工具,搭配100+可扩展技能,成为兼顾实用性与隐私性的AI工具首选。
20597 113

热门文章

最新文章