Java 批量文件压缩导出并下载到本地示例代码
主要用的是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 批量文件压缩导出并下载到本地示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
相关文章
Spring Boot 2.x 把 Guava 干掉了选择本地缓存之王 Caffeine(推荐)
这篇文章主要介绍了Spring Boot 2.x 把 Guava 干掉了选择本地缓存之王 Caffeine,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-01-01
springboot整合easy-es实现数据的增删改查的示例代码
Easy-Es是一款基于ElasticSearch官方提供的RestHighLevelClient打造的低码开发框架,本文主要介绍了springboot整合easy-es实现数据的增删改查的示例代码,具有一定的参考价值,感兴趣的可以了解一下2024-03-03
SpringBoot+SpringSecurity+JWT实现系统认证与授权示例
本文主要介绍了SpringBoot+SpringSecurity+JWT实现系统认证与授权示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-08-08
Spring Data JPA实现持久化存储数据到数据库的示例代码
Spring Data JPA是Spring基于JPA规范的基础上封装的⼀套 JPA 应⽤框架,可使开发者⽤极简的代码即可实现对数据库的访问和操作。本文我们来了解如何用Spring Data JPA框架实现数据持久化存储到数据库,感兴趣的可以了解一下2022-04-04


最新评论