Java 批量文件壓縮導出并下載到本地示例代碼
主要用的是org.apache.tools.zip.ZipOutputStream 這個zip流,這里以Execl為例子。
思路首先把zip流寫入到http響應輸出流中,再把excel的流寫入zip流中(這里可以不用生成文件再打包,只需把execl模板讀出寫好數(shù)據(jù)輸出到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);//...處理文件內(nèi)容操作
ZipEntry zipEntry = new ZipEntry(new String("文件名XXX".getBytes(),"utf-8")+".xls"); //自己命名,這里假設是1,2,3
zipStream.putNextEntry(zipEntry);
wb.write(zipStream);//這里就是循環(huán)每次把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);
}
}
最后的結(jié)果生成一個報表.zip,其中包含3個文件1.xls,2.xls,3.xls
總結(jié)
以上所述是小編給大家介紹的Java 批量文件壓縮導出并下載到本地示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine(推薦)
這篇文章主要介紹了Spring Boot 2.x 把 Guava 干掉了選擇本地緩存之王 Caffeine,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
springboot整合easy-es實現(xiàn)數(shù)據(jù)的增刪改查的示例代碼
Easy-Es是一款基于ElasticSearch官方提供的RestHighLevelClient打造的低碼開發(fā)框架,本文主要介紹了springboot整合easy-es實現(xiàn)數(shù)據(jù)的增刪改查的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Spring使用@Conditional進行條件裝配的實現(xiàn)
在spring中有些bean需要滿足某些環(huán)境條件才創(chuàng)建某個bean,這個時候可以在bean定義上使用@Conditional注解來修飾,所以本文給大家介紹了Spring使用@Conditional進行條件裝配的實現(xiàn),文中通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2023-12-12
java連接SQL Server數(shù)據(jù)庫的方法
這篇文章主要為大家詳細介紹了java連接SQL Server數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Java?SpringBoot?獲取接口實現(xiàn)類匯總
這篇文章主要介紹了Java?SpringBoot?獲取接口實現(xiàn)類匯總,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
SpringBoot+SpringSecurity+JWT實現(xiàn)系統(tǒng)認證與授權(quán)示例
本文主要介紹了SpringBoot+SpringSecurity+JWT實現(xiàn)系統(tǒng)認證與授權(quán)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Spring?Data?JPA實現(xiàn)持久化存儲數(shù)據(jù)到數(shù)據(jù)庫的示例代碼
Spring Data JPA是Spring基于JPA規(guī)范的基礎上封裝的?套 JPA 應?框架,可使開發(fā)者?極簡的代碼即可實現(xiàn)對數(shù)據(jù)庫的訪問和操作。本文我們來了解如何用Spring?Data?JPA框架實現(xiàn)數(shù)據(jù)持久化存儲到數(shù)據(jù)庫,感興趣的可以了解一下2022-04-04

