strut2文件下载三部曲:一个流、两个头信息

说明:

①一个流,在Action中需要一个getInputStream()的方法来获取下载的内容,其中inputStream是默认的,他会指示StreamResult获得inputStream属性的getter方法。

②两个头,一个为ContentType:默认rext/plain文件形式。主要作用是根据下载的文件类型进行文件设置,需要的任何MIME可以在tomcat里面的配置文件中找到

另一个头是ContentDisposition:默认inline,直接在网页上打开,我们一般把他设置为attachment,弹出窗口查看下载信息和选择下载路径

以下用导出excel数据表格为例:

一、首先在查询出所需要的数据时,进数据放到session域中

servletActionContext.getRequest().getSession().setAttribute("list",list);

二、在页面设置一个按钮

<a href="${pageContext.request.contextPath}/user_exportXls">导出</a>

三、配置struts.xml文件

<action  name="user_*" method="{1}" class="Action的全类名">

<result name="exportXlsSUCCESS" type="stream">

<param name="contentType">application/vnd.ms-excel</param>   <!--excel文件类型-->
       <param name="contentDisposition">attachment;filename=用户数据.xls</param> <!--下载弹窗和下载文件名-->

</result>

</action>

四、Action类 实现getInputStream方法

public String  exportXls()

{

return "exportXlsSUCCESS";

}

public InputStream getInputStram() throws IOException

{

// 将 userData缓存缓存在Session中的数据 ,生成Excel

List<User> userData = (User) ServletActionContext.getRequest().getSession().getAttribute("User");

// 根据内存的数据生成Excel   // 工作薄

HSSFWorkbook hssfWorkbook = new HSSFWorkbook();

// 生成一张表sheet   (表名为:用户数据)

HSSFSheet sheet = hssfWorkbook.createSheet("用户数据");

// 先写标题行

HSSFRow headRow = sheet.createRow(0);

// 第一行 (标题行,也叫表头)

headRow.createCell(0).setCellValue("编号");

headRow.createCell(1).setCellValue("用户名");

headRow.createCell(2).setCellValue("性别");

headRow.createCell(3).setCellValue("爱好");

headRow.createCell(4).setCellValue("电话号码");

headRow.createCell(5).setCellValue("住址信息");

..................................等

// 向excel写数据

for (User user: userData)

{

// 每个分区一行

HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);   //  获取已经存在的最后一行的行数,在这行下再创建新的行

dataRow.createCell(0).setCellValue(user.getId());

dataRow.createCell(1).setCellValue(user.getUsername());

dataRow.createCell(2).setCellValue(user.getGender());

dataRow.createCell(3).setCellValue(user.getHobby());

dataRow.createCell(4).setCellValue(user.getTelephone());

dataRow.createCell(5).setCellValue(user.getAddr());

.....等

}

// 将数据缓存到字节数组 (知识点)

ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();

hssfWorkbook.write(arrayOutputStream);

arrayOutputStream.close();

byte[] data = arrayOutputStream.toByteArray();

// 再通过字节数组输入流读取数据

return new ByteArrayInputStream(data);

}

五、知识点,如何将缓存中的数据转换为输入流?

//实例化一个字节数组输出流

ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();

// 将工作簿的数据写入到字节数组输出流中

hssfWorkbook.write(arrayOutputStream);

//将字节数组输出流关闭

arrayOutputStream.close();

//将字节数组输出流转换为字节流

byte[] data = arrayOutputStream.toByteArray();

// 再通过字节数组输入流读取数据

InputStream inputStream = new ByteArrayInputStream(data);

最新文章

  1. angularJS中的Promise对象($q)的深入理解
  2. Java 2D API - 2. Graphics 入门
  3. hdu4044 GeoDefense
  4. FreeBSD 9.1安装KMS 这是一个伪命题###### ,9....
  5. mysqldump 数据库迁移并改换engine
  6. 疯狂java讲义——继承
  7. IO输入输出 3
  8. java实现最基础的socket网络通信
  9. mac 功能修改。。。。
  10. java项目
  11. Wireshark 基本介绍和学习 TCP 三次握手
  12. unix cd使用命令
  13. python实现维吉利亚密码加密(Vigen&#232;re cipher)
  14. XMPP系列(六)---创建群组
  15. java反射机制--reflection
  16. JAVA基础第四章-集合框架Collection篇
  17. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
  18. datatable 转list ,list转datatable
  19. spring BeanFactory及ApplicationContext中Bean的生命周期
  20. February 2nd, 2018 Week 5th Friday

热门文章

  1. java 获取昨天日期
  2. plsql 存储过程 测试
  3. eclipse could not create the Java Vitual Machine
  4. 〖Linux〗Qt5.2.0+gsoap开发Android的NDK程序遇到错误的解决
  5. webservice系统学习笔记1-使用注解创建ws服务
  6. python之smtplib模块 发送邮件
  7. windows执行命令来运行loadrunner录制好的脚本(收藏)
  8. java操作hdfs到数据库或者缓存
  9. iOS获取手机型号,Swift获取手机型号(类似iphone 7这种,检测机型具体型号)
  10. jsp中Undefined type: xxxx...