1. 程式人生 > >批量檔案打包下載詳細程式碼解析(下載即可用)

批量檔案打包下載詳細程式碼解析(下載即可用)

進來的各位大佬們幫忙點選一下淘寶連結(幫家人的店鋪增加點兒人氣),感謝感謝!!!

進來的各位大佬們幫忙點選一下淘寶連結(幫家人的店鋪增加點兒人氣),感謝感謝!!!

進來的各位大佬們幫忙點選一下淘寶連結(幫家人的店鋪增加點兒人氣),感謝感謝!!!

進來的各位大佬們幫忙點選一下淘寶連結(幫家人的店鋪增加點兒人氣),感謝感謝!!!


一:批量檔案打包下載詳細程式碼解析
多檔案打包下載的重點在於如何將檔案進行打包。在專案中體現的流程便是:獲取待下載的檔案路徑->新增進zip檔案->返回zip路徑->zip檔案下載。
我們一步一步來:

在程式中匯入壓縮檔案所需要的jar包:
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.18
</version> </dependency> public String downloadMultiFile() throws IOException { //獲取將要下載的檔案路徑 String urls = this.getHttpServletRequest().getParameter("attachment"); String[] files = urls.split(","); // String[] files = {"/2018-08/84d378c9-6eb4-41c5-b16a-87538603b327_菲律賓邀請函.doc","/2018-08/f3f86879-dbd2-4b18-aa75-74135ab4bd48_打表測試.docx"};
//使用預設檔名稱 String name = "檔案壓縮包.zip"; //壓縮後的zip檔案存放路徑 //使用File.separator代替"\\",避免Linux系統解析成/ String fileName = "C:"+File.separator+"uploadMeterial"+File.separator+name+""; System.out.println("壓縮檔案路徑:"+fileName); //獲取打包下載的路徑 File[] filestr = new
File[files.length]; for(int i = 0;i<files.length;i++){ if(files[i] != null){ String filepath = "C:"+File.separator+"uploadMeterial"+files[i]; File file = new File(filepath);//獲取到檔案指標 if(file.exists()) { System.out.println("獲取檔案指標---"+filepath+"---成功"); filestr[i] = file; } } } //將多個附件壓縮成zip到指定路徑 System.out.println("開始壓縮檔案...."); ZipFileUtil.compressFiles2Zip(filestr,fileName); fileDownloads(fileName); //fileName = name;//zip檔名稱,zip下載的action中有前面省略的路徑,可自行更改 return SUCCESS; } public String fileDownloads(String fileName) throws IOException { HttpServletResponse response = this.getHttpServletResponse(); String fileSaveRootPath = fileName; //得到要下載的檔案 File file = new File(fileSaveRootPath); //如果檔案不存在 if(!file.exists()){ resultList.put("status",210); resultList.put("failMesg","您要下載的資源已被刪除"); return SUCCESS; } fileName = "材料打包檔案.zip"; //設定響應頭,控制瀏覽器下載該檔案 this.getHttpServletResponse().reset(); response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); //讀取要下載的檔案,儲存到檔案輸入流 FileInputStream in = new FileInputStream(fileSaveRootPath); //建立輸出流 OutputStream out = response.getOutputStream(); //建立緩衝區 byte buffer[] = new byte[1024]; int len = 0; //迴圈將輸入流中的內容讀取到緩衝區當中 while((len=in.read(buffer))>0){ //輸出緩衝區的內容到瀏覽器,實現檔案下載 out.write(buffer, 0, len); } //關閉檔案輸入流 in.close(); //關閉輸出流 out.close(); resultList.put("status",200); return SUCCESS; } 這裡是ZipFileUtil類原始碼: public class ZipFileUtil { /** * 把檔案壓縮成zip格式 * @param files 需要壓縮的檔案 * @param zipFilePath 壓縮後的zip檔案路徑 ,如"D:/test/aa.zip"; */ public static void compressFiles2Zip(File[] files, String zipFilePath) { if(files != null && files.length >0) { if (isEndsWithZip(zipFilePath)) { ZipArchiveOutputStream zaos = null; try { File zipFile = new File(zipFilePath); zaos = new ZipArchiveOutputStream(zipFile); zaos.setUseZip64(Zip64Mode.AsNeeded); //將每個檔案用ZipArchiveEntry封裝,再用ZipArchiveOutputStream寫到壓縮檔案中 for (File file : files) { System.out.println("壓縮..."); if (file != null) { ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName()); zaos.putArchiveEntry(zipArchiveEntry); InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024 * 5]; int len = -1; while ((len = is.read(buffer)) != -1) { //把緩衝區的位元組寫入到ZipArchiveEntry zaos.write(buffer, 0, len); } //Writes all necessary data for this entry. zaos.closeArchiveEntry(); } catch (Exception e) { throw new RuntimeException(e); } finally { if (is != null) is.close(); } } } zaos.finish(); } catch (Exception e) { throw new RuntimeException(e); } finally { try { if (zaos != null) { zaos.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } System.out.println("檔案壓縮完成w."); } } /** * 判斷檔名是否以.zip為字尾 * @param fileName 需要判斷的檔名 * @return 是zip檔案返回true,否則返回false */ public static boolean isEndsWithZip(String fileName) { boolean flag = false; if(fileName != null && !"".equals(fileName.trim())) { if(fileName.endsWith(".ZIP")||fileName.endsWith(".zip")){ flag = true; } } return flag; } }

借鑑該篇文章:https://blog.csdn.net/csu_passer/article/details/78202518