Java 批量檔案壓縮匯出,並下載到本地

來源:互聯網
上載者:User

標籤:extent   stc   包含   tor   ica   ati   header   null   content   

  主要用的是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 批量檔案壓縮匯出,並下載到本地

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.