欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-17 16:13:40
主要用的是org.apache.tools.zip.zipoutputstream  这个zip流,这里以execl为例子。 思路首先把zip流写入到http响...

主要用的是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 批量文件压缩导出并下载到本地示例代码,希望对大家有所帮助

上一篇:

下一篇: