日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java多文件文件压缩加密下载

發布時間:2023/12/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java多文件文件压缩加密下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求: 網絡多文件下載并壓縮,然后對壓縮文件進行加密操作。

一、基本思路:

1. 循環URL list通過RUI獲取文件流生成本地文件,add進文件集合

2. zip文件 add普通文件集合,生成壓縮文件

3. response 下載。通過壓縮文件路徑獲取文件流,寫入outputstream

二、代碼實現

maven依賴

<dependency><groupId>net.lingala.zip4j</groupId><artifactId>zip4j</artifactId><version>1.3.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency>

下載文件代碼如下:

import cn.hutool.core.date.DateUtil; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.springframework.util.ResourceUtils;import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URI; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List;/*** * @param response* @param urls 文件url* @param zipName 壓縮文件名稱*/public static void downloadZip(HttpServletResponse response, List<String> urls, String zipName) {try {String filePath = "~/Downloads/";String fileName = zipName + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".zip";ZipFile zipFile = getZipStream(urls, UserUtil.getUserAccount(), filePath + fileName);InputStream fis = new BufferedInputStream(new FileInputStream(filePath + fileName));response.setCharacterEncoding("UTF-8");response.setHeader("Content-Disposition","attachment; filename=" + new String(fileName.getBytes("ISO8859-1"), "UTF-8"));response.setContentLength((int) zipFile.getFile().length());response.setContentType("application/zip");// 定義輸出類型BufferedInputStream buff = new BufferedInputStream(fis);byte[] b = new byte[1024];// 相當于我們的緩存long k = 0;// 該值用于計算當前實際下載了多少字節OutputStream myout = response.getOutputStream();// 從response對象中得到輸出流,準備下載// 開始循環下載while (k < zipFile.getFile().length()) {int j = buff.read(b, 0, 1024);k += j;myout.write(b, 0, j);}myout.flush();buff.close();zipFile.getFile().delete();} catch (Exception e) {e.printStackTrace();}}/*** * @param fileUrls 文件urls* @param password 壓縮加密字符串* @param zipFileName 壓縮包全路徑(包括路徑和文件名)* @return* @throws IOException* @throws ZipException*/public static ZipFile getZipStream(List<String> fileUrls, String password, String zipFileName) throws IOException, ZipException {if (fileUrls != null && fileUrls.size() > 0) {ArrayList<File> filesToAdd = new ArrayList<>();for (String fileUrl : fileUrls) {URI uri = URI.create(fileUrl);String name = FilenameUtils.getName(uri.getPath());System.out.println(name);File file = new File("~/Downloads/" + name);FileUtils.copyInputStreamToFile(uri.toURL().openStream(), file);filesToAdd.add(file);}ZipParameters parameters = new ZipParameters();parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);//壓縮方式parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);//壓縮級別parameters.setEncryptFiles(true);parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);//加密方式parameters.setPassword(password);ZipFile zipFile = new ZipFile(zipFileName);zipFile.setFileNameCharset("gbk");zipFile.addFiles(filesToAdd, parameters);if (filesToAdd.size() > 0) {for (File file : filesToAdd) {file.delete();}}return zipFile;}return null;}

總結

以上是生活随笔為你收集整理的java多文件文件压缩加密下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。