使用struts2的方式完成下载

对于下载excel2003,contentType如此设置

<result name="success" type="stream">
       <param name="contentType">application/vnd.ms-excel</param>
       <param name="inputName">inputStream</param>
       <param name="contentDisposition">attachment;filename="${#request.filename}.xls"</param>

  <!-- <param name="contentDisposition">attachment;filename="${fileName}"</param> -->
       <param name="bufferSize">1024</param>
   </result>

对于下载excel2007,contentType如此设置

<param name="contentType">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</param>

注:

1.<param name="contentDisposition">中的attachment表示以附件形式保存到浏览器,而不是直接输在浏览器页面,${fileName} 为action中的文件名变量,action中需要声明该变量并写getter和setter方法

2.<param name="inputName">中的inputStream为输入流变量名,需要在action中声明并写getter和setter方法

在action中

  private InputStream inputStream;//输入流变量

  private String fileName;//下载文件名

关键代码:

public String download(){
  try {
  //获取模板文件的id,通过id,来查询出模板文件的信息,获取路径path
  Integer id=applicationTemplate.getId();
  ApplicationTemplate applicationTemplate=applicationTemplateService.findApplicationTemplateById(id);
  //获取路径path
 // String path=applicationTemplate.getPath();
  String path = "/download";
  //将路径path转成输入流
   InputStream in=new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("")+path));
   //将输入流的数据放置到模型驱动对象的InputStream的属性中
   applicationTemplate.setInputStream(in);
   
   //获取下载文件的名字
   String filename = applicationTemplate.getName();
   filename = new String(filename.getBytes("gbk"),"iso-8859-1");
   request.setAttribute("filename", filename);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
  
  return "success";
 }

--------------------------------------------------------------------------------------

* @Return: 一:使用javaweb的方式完成下载,return null
 */
// public String download(){
//  try {
//   //1:获取传递的申请模板ID,使用申请模板ID,查询申请模板信息,获取路径path
//   Integer id = elecApplicationTemplate.getId();
//   ElecApplicationTemplate applicationTemplate = elecApplicationTemplateService.findApplicationTemplateByID(id);
//   //获取路径path
//   String path = applicationTemplate.getPath();
//   //2:将路径path转化成输入流,将输入流的信息,写到输出流(从response对象中获取输出流)
//   InputStream in = new FileInputStream(new File(ServletActionContext.getServletContext().getRealPath("")+path));
//   
//   //获取申请模板文件的文件名
//   String filename = applicationTemplate.getName();
//   filename = new String(filename.getBytes("gbk"),"iso-8859-1");
//   
//   //设置文件下载的格式
//   response.setContentType("application/msword");
//   //设置附件的数据处理方式
//   response.setHeader("Content-disposition", "attachment;filename="+filename+".doc");
//   //设置下载附件的缓冲区大小
//   response.setBufferSize(1024);
//   
//   OutputStream out = response.getOutputStream();
//   for(int b=-1;(b=in.read())!=-1;){
//    out.write(b);
//   }
//   out.close();
//   in.close();
//  } catch (Exception e) {
//   e.printStackTrace();
//  }
//  return null;
// }

下载excel的更多相关文章

  1. 360浏览器下载excel问题解决方式

    亲们有没有碰到过今天我遇到的这件事. 如果使用简单的链接.或者get方式提交的表单,去下载excel,那么360浏览器就会有问题. 问题是:它没把我用java生成的excel表格下载,而是去把我的列表 ...

  2. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  3. 在线读取Mongodb数据库下载EXCEL文件

    版本:Mongodb2.4.8 通过页面下载Excel文件 jsp <%@ page language="java" contentType="text/html; ...

  4. angularJS通过post方法下载excel文件

    最近工作中遇到,要使用angularJS的post方法来下载excel的情况.网上找到一个帖子:http://stackoverflow.com/questions/22447952/angularj ...

  5. asp.net 下载Excel (数据流,不保存)--客户端

    效果图: 前端页面 <html> <head> <title>Test For Excel</title> <script src="j ...

  6. Angularjs 通过WebApi 下载excel

    如果想知道 AngularJs 通过WebAPI 下载Excel.请看下文,这里仅提供了一种方案. 服务器端代码如下: protected HttpResponseMessage GenereateE ...

  7. 使用DateSet下载Excel

    这里我们使用Microsoft.Office.Interop.Excel.dll下载Excel,没有引用可点击下载 关键代码,ExcelHelper类 using System; using Syst ...

  8. 前端axios下载excel(二进制)

    需求:通过后端接口下载excel文件,后端没有文件地址,返回二进制流文件 实现:axios(ajax类似) 主要代码: axios:设置返回数据格式为blob或者arraybuffer 如: var ...

  9. 前端axios下载excel,并解决axios返回header无法获取所有数据的问题

    需求:通过后端接口下载excel文件,后端没有文件地址,返回二进制流文件 实现:axios(ajax类似) 主要代码: axios:设置返回数据格式为blob或者arraybuffer 如: var ...

  10. C# 之 下载EXCEL文件,自动用迅雷下载aspx

    在浏览器中导出 Excel 得时候,如果浏览器绑定了迅雷,则会下载aspx文件. 解决:下载EXCEL文件,自动用迅雷下载aspx if (Request.QueryString["id&q ...

随机推荐

  1. tkinter获取键盘输入

    tkinter获取键盘输入

  2. 17.1拓展之纯 CSS 创作炫酷的同心圆旋转动画

    效果地址:https://codepen.io/flyingliao/pen/ebjEMm?editors=1100 HTML代码: <div class="loader"& ...

  3. 《算法导论》——MaximumSubArray

    今天我们讨论的算法是最大子数组问题. 首先我定义了一个类用来保存最大子数组的开始位置索引.结束位置索引和该数组的和.代码如下: class MaximumSubArray { private: int ...

  4. hive 索引

    hive 有限的支持索引,不支持主键外键,可以对表添加索引,也可以为某个分区添加索引.维护索引也要额外的存储空间和计算资源. 创建索引需要指定索引处理器 如 as 'org.apache.hadoop ...

  5. 构建最小JDK Docker镜像

    参考: https://my.oschina.net/shyloveliyi/blog/1627020 1.首先下载jre,下载地址是https://www.java.com/en/download/ ...

  6. DDD随笔-Axon

    1. 命令处理程序从存储库中检索域对象(聚合)并执行它们的方法来更改它们的状态.这些聚合通常包含实际的业务逻辑,因此负责维护自己的状态.聚合的状态变化导致产生领域事件.领域事件和聚合形成领域模型. 2 ...

  7. leetcode991

    class Solution: def brokenCalc(self, X: 'int', Y: 'int') -> 'int': if X>=Y : return Y-X else: ...

  8. NRF51822之FICR

    uint32_t deviceID[2];union{    uint32_t  DW;    uint8_t   B[4];} device_ID;//read device ID deviceID ...

  9. 火狐Firefox浏览器所有历史版本下载地址

    Mozilla Firefox 频繁的更新,导致许多好用的插件在更新后不能兼容,而且想换回低版本还不容易啊,官网上只看到最新版本和前一个版本的下载. 这里为大家提供了一个下载链接,是来自Mozilla ...

  10. 基于Redis的Spring cache 缓存介绍

    目录 Cache API及默认提供的实现 demo 依赖包安装 定义实体类.服务类和相关配置文件 Cache注解 启用Cache注解 @CachePut @CacheEvict @Cacheable ...