java實現文件打包壓縮輸出到瀏覽器下載
更新時間:2021年11月17日 09:53:51 作者:Java大表哥
這篇文章主要介紹了java實現文件打包壓縮輸出到瀏覽器下載,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
文件打包壓縮輸出到瀏覽器下載
java批量下載文件打包壓縮工具類,輸出到瀏覽器下載,可以自己改名。
一、工具類:
入參 :文件LIst ;打包后的名字 ;響應到瀏覽器
/** * 功能:壓縮多個文件,輸出壓縮后的zip文件流 * * @param srcfile:源文件列表 * @param zipFileName:壓縮后的文件名 * @param response: Http響應 */ public void zipFiles(List<File> srcfile, String zipFileName, HttpServletResponse response) throws IOException { byte[] buf = new byte[1024]; // 獲取輸出流 BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } FileInputStream in = null; ZipOutputStream out = null; try { response.reset(); // 重點突出 // 不同類型的文件對應不同的MIME類型 response.setContentType("application/x-msdownload"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + zipFileName + ".zip"); // ZipOutputStream類:完成文件或文件夾的壓縮 out = new ZipOutputStream(bos); for (int i = 0; i < srcfile.size(); i++) { in = new FileInputStream(srcfile.get(i)); // 給列表中的文件單獨命名 out.putNextEntry(new ZipEntry(srcfile.get(i).getName())); int len = -1; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } } out.close(); bos.close(); log.info("壓縮完成."); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) in.close(); if (out != null) out.close(); } }
二、調用
zipFiles(files, zipName, response);
生成zip文件并瀏覽器導出
總結一下,關于Java下載zip文件并導出的方法,瀏覽器導出。
String downloadName = "下載文件名稱.zip"; downloadName = BrowserCharCodeUtils.browserCharCodeFun(request, downloadName);//下載文件名亂碼問題解決 //將文件進行打包下載 try { OutputStream out = response.getOutputStream(); byte[] data = createZip("/fileStorage/download");//服務器存儲地址 response.reset(); response.setHeader("Content-Disposition","attachment;fileName="+downloadName); response.addHeader("Content-Length", ""+data.length); response.setContentType("application/octet-stream;charset=UTF-8"); IOUtils.write(data, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); }
獲取下載zip文件流
public byte[] createZip(String srcSource) throws Exception{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); //將目標文件打包成zip導出 File file = new File(srcSource); a(zip,file,""); IOUtils.closeQuietly(zip); return outputStream.toByteArray(); }
public void a(ZipOutputStream zip, File file, String dir) throws Exception { //如果當前的是文件夾,則進行進一步處理 if (file.isDirectory()) { //得到文件列表信息 File[] files = file.listFiles(); //將文件夾添加到下一級打包目錄 zip.putNextEntry(new ZipEntry(dir + "/")); dir = dir.length() == 0 ? "" : dir + "/"; //循環(huán)將文件夾中的文件打包 for (int i = 0; i < files.length; i++) { a(zip, files[i], dir + files[i].getName()); //遞歸處理 } } else { //當前的是文件,打包處理 //文件輸入流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(dir); zip.putNextEntry(entry); zip.write(FileUtils.readFileToByteArray(file)); IOUtils.closeQuietly(bis); zip.flush(); zip.closeEntry(); } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
舉例講解Java編程中this關鍵字與super關鍵字的用法
這篇文章主要介紹了Java編程中this關鍵字與super關鍵字的用法示例,super是this的父輩,在繼承過程中兩個關鍵字經常被用到,需要的朋友可以參考下2016-03-03Intellij IDEA 2017.3使用Lombok及常用注解介紹
這篇文章主要介紹了Intellij IDEA 2017.3使用Lombok及常用注解介紹,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09tio-boot?jfinal-plugins框架整合redis示例詳解
這篇文章主要為大家介紹了tio-boot?jfinal-plugins框架整合redis示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12