利用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等工具,开发者能提升用户体验,确保输入框在各种场景下的良好表现。实例分析和实践建议有助于开发者将这些方法应用于实际项目。
780 0
【Flutter 前端技术开发专栏】Flutter 中的键盘处理与输入框优化
|
设计模式 关系型数据库 Java
顺畅的职责传递-用责任链模式优化决策流程
本文首先通过经典场景展示了不使用设计模式时的问题与痛点。接着,引入责任链模式,详细讲解了其定义、解决问题的方式、结构图及工作原理,并通过重构示例展示了该模式如何解决原有痛点。最后,对责任链模式的优势、缺点以及在实际应用中可能遇到的挑战和限制进行了总结。责任链模式通过解耦请求发送者和接收者,提供了灵活的请求处理机制,适用于多个处理者按顺序处理请求的场景。然而,该模式也可能导致请求得不到处理或性能下降等问题,需在实际应用中权衡利弊。
858 0
顺畅的职责传递-用责任链模式优化决策流程
【UI】 修改element-ui input输入框placeholder提示信息、占位符的样式
【UI】 修改element-ui input输入框placeholder提示信息、占位符的样式
1732 0
|
Web App开发 算法
软件破解初级实例教程(附工具附图)
最近在群里总是看到很多新朋友在问: 1、“新手怎么学破解啊?”(这是标准的伸手党,baidu google其实很好用) 2、“哎呀XX大牛,我什么基础都没有啊我不会汇编,不会C更不会C++还不会…………总之高手会的我都不会,我能学么?”(明确的告诉你,你能!你不和唐僧一样罗嗦的话你一定能。
12219 1
|
物联网 智能硬件
IOE与IOT:有什么区别?
互联网的各种用途层出不穷,其中许多已经在我们的智能家居中变得越来越常见。
1456 0
IOE与IOT:有什么区别?
|
SQL 消息中间件 分布式计算
菜鸟供应链实时数仓的架构演进及应用场景
菜鸟数据&规划部高级数据技术专家贾元乔从数据模型、数据计算、数据服务等几个方面介绍了菜鸟供应链数据团队在实时数据技术架构上的演进,以及在供应链场景中典型的实时应用场景和 Flink 的实现方案。
菜鸟供应链实时数仓的架构演进及应用场景
|
20天前
|
人工智能 自然语言处理 Shell
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
32230 117
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
|
9天前
|
应用服务中间件 API 网络安全
3分钟汉化OpenClaw,使用Docker快速部署启动OpenClaw(Clawdbot)教程
2026年全新推出的OpenClaw汉化版,是基于Claude API开发的智能对话系统本土化优化版本,解决了原版英文界面的使用壁垒,实现了界面、文档、指令的全中文适配。该版本采用Docker容器化部署方案,开箱即用,支持Linux、macOS、Windows全平台运行,适配个人、企业、生产等多种使用场景,同时具备灵活的配置选项和强大的扩展能力。本文将从项目简介、部署前准备、快速部署、详细配置、问题排查、监控维护等方面,提供完整的部署与使用指南,文中包含实操代码命令,确保不同技术水平的用户都能快速落地使用。
4726 4
|
15天前
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
6829 18
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手