国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java 批量文件壓縮導出并下載到本地示例代碼

2024-07-13 10:15:32
字體:
來源:轉載
供稿:網友

主要用的是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 批量文件壓縮導出并下載到本地示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 京山县| 祁门县| 恩施市| 安陆市| 鄱阳县| 库尔勒市| 深水埗区| 杂多县| 阿尔山市| 万山特区| 江城| 莱芜市| 郯城县| 庄河市| 孟村| 靖安县| 东乌珠穆沁旗| 渝北区| 兴国县| 通海县| 石柱| 汤阴县| 拜城县| 莲花县| 盐亭县| 贡山| 兴仁县| 武定县| 湖口县| 鄂托克前旗| 富蕴县| 巩留县| 台东县| 高要市| 吴川市| 海城市| 金湖县| 安宁市| 沽源县| 德州市| 嘉兴市|