springboot多文件壓縮實現(xiàn)過程
更新時間:2025年04月21日 08:52:20 作者:little_fairy66
這篇文章主要介紹了springboot多文件壓縮實現(xiàn)過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
springboot多文件壓縮
項目要求需要將所有附件進行壓縮,記錄下步驟
環(huán)境依賴
我使用的時jdk8,基本已經(jīng)包含了所有可用的包,但是若是又特殊要求需要用到的自行導(dǎo)入
實現(xiàn)步驟
直接貼代碼吧,我在本地自己測試用,所以文件路徑都是本地路徑,記得修改
// 多文件壓縮為zip //多個文件,壓縮成zip后下載 public void downloadMoreFile(HttpServletResponse response) { String fileName1= "XXXXX\\Desktop\\2022.1fp\\1.xlsx"; String fileName2= "XXXXX\\Desktop\\2022.1fp\\2.xlsx"; // 創(chuàng)建文件對象 File aFile= new File(fileName1); File bFile= new File(fileName2); // 構(gòu)建文件集合,存放所有待壓縮的文件 List<File> files = new ArrayList<>(); files.add(aFile); files.add(bFile); // 判斷文件存在性,若不存在文件無法壓縮 if (aFile.exists() && bFile.exists()) { // 壓縮包絕對路徑 String zipTmp = "XXXXXX\\Desktop\\2022.1fp\\2.zip"; // 壓縮 zipd(zipTmp,files,response); } }
- zipd實現(xiàn)內(nèi)容
/** * 壓縮 * @param zipTmp 壓縮包絕對路徑 * @param files 待壓縮文件集合 * @param response http請求代表響應(yīng)的對象 */ public void zipd(String zipTmp,List<File> files,HttpServletResponse response){ // 創(chuàng)建壓縮包文件對象 File zipTmpFile = new File(zipTmp); try { // 壓縮包文件若已存在則刪除原本的文件 if (zipTmpFile.exists()) { zipTmpFile.delete(); } // 不存在新建壓縮文件 zipTmpFile.createNewFile(); // 去除jsp編譯html首部的空白行。 response.reset(); // FileOutputStream字節(jié)輸出流超類 文件輸出流是用于將數(shù)據(jù)寫入到輸出流File或一個FileDescriptor // 創(chuàng)建文件輸出流 FileOutputStream fous = new FileOutputStream(zipTmpFile); // ZipOutputStream壓縮流 // 此流用于以 ZIP 文件格式寫入文件,包括對壓縮和未壓縮條目的支持,也就是把文件打包成壓縮文件,常用于附件下載(多文件下載),文件壓縮存儲。 ZipOutputStream zipOut = new ZipOutputStream(fous); // 循環(huán)處理待壓縮文件 int size = files.size(); for (int i = 0; i < size; i++) { File file = files.get(i); // 將文件寫入壓縮流 zipFile(file, zipOut); } // 關(guān)閉流 zipOut.close(); fous.close(); // downloadZip(zipTmpFile, response); } catch (IOException e) { e.printStackTrace(); } }
- zipFile實現(xiàn)內(nèi)容
/** * * @param inputFile 待壓縮文件 * @param ouputStream 壓縮流 */ public void zipFile(File inputFile, ZipOutputStream ouputStream) { try { if (inputFile.exists()) { if (inputFile.isFile()) { // 待壓縮文件輸入流 FileInputStream IN = new FileInputStream(inputFile); // 創(chuàng)建 BufferedInputStream具有指定緩沖區(qū)大小,并保存其參數(shù),輸入流 in ,供以后使用。 BufferedInputStream bins = new BufferedInputStream(IN, 512); // ZipEnter:表示壓縮文件的條目(文件目錄) ZipEntry entry = new ZipEntry(inputFile.getName()); // 在zip流中創(chuàng)建一個entry文件 ouputStream.putNextEntry(entry); int nNumber; byte[] buffer = new byte[512]; while ((nNumber = bins.read(buffer)) != -1) { ouputStream.write(buffer, 0, nNumber); } // 關(guān)閉流 bins.close(); IN.close(); } else { // 處理文件夾的情況 try { File[] files = inputFile.listFiles(); for (int i = 0; i < files.length; i++) { zipFile(files[i], ouputStream); } } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } }
若需要下載,記得加后面的方法
public static HttpServletResponse downloadZip(File file, HttpServletResponse response) { if (file.exists() == false) { System.out.println("待壓縮的文件目錄:" + file + "不存在."); } else { try { // 以流的形式下載文件。 InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); // 如果輸出的是中文名的文件,在此處就要用URLEncoder.encode方法進行處理 response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("GB2312"), "ISO8859-1")); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { try { File f = new File(file.getPath()); f.delete(); } catch (Exception e) { e.printStackTrace(); } } } return response; }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot集成redis哨兵集群的實現(xiàn)示例
本文主要介紹了springboot集成redis哨兵集群的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08springboot+spring?data?jpa實現(xiàn)新增及批量新增方式
這篇文章主要介紹了springboot+spring?data?jpa實現(xiàn)新增及批量新增方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Java 數(shù)組聲明、創(chuàng)建、初始化詳解
本文主要介紹Java 數(shù)組聲明、創(chuàng)建、初始化的資料,這里整理相關(guān)知識,及簡單實現(xiàn)代碼,幫助大家學(xué)習(xí),有興趣的小伙伴可以參考下2016-09-09專屬于程序員的浪漫-Java輸出動態(tài)閃圖iloveyou
這篇文章主要介紹了專屬于程序員的浪漫-Java輸出動態(tài)閃圖iloveyou,具有一定參考價值,需要的朋友可以了解下。2017-11-11