Java 批量文件压缩导出并下载到本地示例代码

作者:紫薇帝星的故事 时间:2023-04-15 07:29:30 

主要用的是org.apache.tools.zip.ZipOutputStream  这个zip流,这里以Execl为例子。

思路首先把zip流写入到http响应输出流中,再把excel的流写入zip流中(这里可以不用生成文件再打包,只需把execl模板读出写好数据输出到zip流中,并为每次的流设置文件名)

   例如:在项目webapp下execl文件中 存在1.xls,2.xls,3.xls文件

1.Controller


@RequestMapping(value = "/exportAll",method = RequestMethod.GET)
 public void exportAll() throws IOException{
   try {
     HttpServletResponse response=this.getResponse();
     response.setContentType("application/octet-stream");
     String execlName = "报表";
     response.addHeader("Content-Disposition", "attachment;filename="+new String(execlName.getBytes(),"iso-8859-1") +".zip");
     OutputStream out = response.getOutputStream();
     testService.exportAll(out);
   } catch (Exception e) {
     ....
   }
 }

2.Service


import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public boolean exportAll(OutputStream out){
 ZipOutputStream zipStream = null;
 HSSFWorkbook wb = null;
 try{
   List<Test> datas = testService.getTestData();
   zipStream = new ZipOutputStream(out);//这里是把zip流输出到httpresponse流中
   for(int i=0;i<3;i++){
    wb = POIUtil.getWorkbook(i);//获取0,1,2.xls文件
    HSSFSheet sheet = wb.getSheetAt(0);
    testService.setSheet(sheet,datas);//...处理文件内容操作
    ZipEntry zipEntry = new ZipEntry(new String("文件名XXX".getBytes(),"utf-8")+".xls"); //自己命名,这里假设是1,2,3
    zipStream.putNextEntry(zipEntry);
    wb.write(zipStream);//这里就是循环每次把execl写入zip包中
    zipStream.flush();
}
   }catch (Exception e) {
     throw new SysException(ERRORConstants.COMMON_SYSTEM_ERROR, e);
   }finally {
      try {
       if(wb!=null){
          wb.close();
       }
       if(zipStream!=null){
         zipStream.close();
       }
       out.flush();
       out.close();
     } catch (IOException e) {
       throw new SysException(ERRORConstants.COMMON_CLOSE_ERROR, e);
     }
   }
 }    
public static HSSFWorkbook getWorkbook(String bh){
try {
String line = File.separator;
ServletRequestAttributes aRequestAttributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request =aRequestAttributes==null?null:aRequestAttributes.getRequest();
String webpath=request.getServletContext().getRealPath("/");
File file = new File(webpath+line+"excel"+line+bh+".xls");
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook wb = new HSSFWorkbook(poifsFileSystem);
return wb;
} catch (Exception e) {
throw new SysException(ERRORConstants.COMMON_SYSTEM_ERROR,e);
}
}

最后的结果生成一个报表.zip,其中包含3个文件1.xls,2.xls,3.xls

总结

以上所述是小编给大家介绍的Java 批量文件压缩导出并下载到本地示例代码网站的支持!

来源:https://www.cnblogs.com/zwdx/archive/2017/12/26/8118379.html

标签:java,文件,压缩,下载
0
投稿

猜你喜欢

  • java如何从不规则的字符串中截取出日期

    2022-05-28 02:33:58
  • SpringMVC中的handlerMappings对象用法

    2023-01-09 21:38:36
  • SpringBoot数据层测试事务回滚的实现流程

    2022-05-01 14:36:37
  • Android 中的危险权限详细整理

    2023-12-03 09:07:25
  • 浅谈Android onTouchEvent 与 onInterceptTouchEvent的区别详解

    2021-05-30 02:56:55
  • 浅谈C#中Md5和Sha1两种加密方式

    2023-04-01 20:25:11
  • C# WinForm 判断程序是否已经在运行,且只允许运行一个实例,附源码

    2021-11-06 14:10:07
  • springboot与mybatis整合实例详解(完美融合)

    2023-01-14 09:25:33
  • Java 开启多线程常见的4种方法

    2023-11-23 02:30:10
  • Android实现recyclerview城市字母索引列表

    2023-09-28 04:22:06
  • SpringBoot框架集成ElasticSearch实现过程示例详解

    2023-02-02 08:44:05
  • java Swing组件setBounds()简单用法实例分析

    2023-11-23 13:35:54
  • Java Fluent Mybatis实战之构建项目与代码生成篇下

    2023-11-24 10:59:52
  • MyBatis源码浅析(一)开篇

    2022-09-28 03:28:24
  • 在Spring Boot中实现HTTP缓存的方法

    2023-10-06 14:18:16
  • java打印菱形及直角和等腰三角形的方法

    2023-01-08 20:46:55
  • SpringBoot下的值注入(推荐)

    2023-04-01 04:45:01
  • C# 图片格式转换的实例代码

    2023-03-11 13:32:46
  • 如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    2023-02-12 21:25:59
  • C#实现Base64处理的加密解密,编码解码示例

    2023-07-15 12:11:31
  • asp之家 软件编程 m.aspxhome.com