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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

通过文件流转加密压缩文件并下载

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

通過文件流轉加密壓縮文件并下載

前言

????

代碼實現

文件流轉ZIP壓縮文件

???? 文件流轉ZIP壓縮文件代碼實現

public void downloadTest(ReqDTO dto, HttpServletResponse response) {try {// 配置Minio訪問下載文件MinioClient minioClient = new MinioClient("http://www.minio.com/", "user", "pwd123#");boolean bucketExist = minioClient.bucketExists("bucketOne");if (!bucketExist) {log.info("create bucket:{}", "bucketOne");minioClient.makeBucket("bucketOne");}final ObjectStat stat = minioClient.statObject("bucketOne", "userInfo.xlsx");log.info("ObjectStat: {}", stat);response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("test.zip", "UTF-8"));// 獲取文件流InputStream inputStream = minioClient.getObject("bucketOne", "userInfo.xlsx");//設置讀取數據緩存大小byte[] buffer = new byte[4096];try {OutputStream os = response.getOutputStream();ZipOutputStream zos = new ZipOutputStream(os);ZipEntry entry = new ZipEntry("userInfo.xlsx");zos.putNextEntry(entry);//寫入壓縮文件int size = 0;//設置讀取數據緩存大小while ((size = inputStream.read(buffer)) > 0) {zos.write(buffer, 0, size);}//關閉輸入輸出流inputStream.close();zos.closeEntry();zos.close();} catch (ZipException e) {log.error("download zipOutputStream error:{}", e.getMessage(), e);e.printStackTrace();}} catch (Exception e) {log.error("download minio error:{}", e.getMessage(), e);throw new BusinessException(ErrorCodeEnum.DOWNLOAD_FAIL);}}

文件流轉加密壓縮文件

???? 文件流轉加密壓縮文件代碼實現

import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.io.ZipOutputStream; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants;private void zipEncryptionFile(ZipOutputStream zos,String fileMd5) {try {MinioClient minioClient = new MinioClient(endpoint, username, secretKey);boolean bucketExist = minioClient.bucketExists(BUCKET_NAME);if (!bucketExist) {log.info("bucket not exit:{}", BUCKET_NAME);}final ObjectStat stat = minioClient.statObject(BUCKET_NAME, fileMd5);log.info("ObjectStat: {}", stat);// 根據文件MD5獲取文件流InputStream inputStream = minioClient.getObject(BUCKET_NAME, fileMd5);// 設置讀取數據緩存大小byte[] buffer = new byte[4096];try {ZipParameters para = new ZipParameters();// 壓縮方式para.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);para.setFileNameInZip(fileMd5);para.setSourceExternalStream(true);// 壓縮級別para.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);para.setEncryptFiles(true);para.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);// 壓縮加密para.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);para.setPassword(ZIP_WORD);zos.putNextEntry(new File(FilenameUtils.getName(fileMd5)), para);// 寫入壓縮文件,設置讀取數據緩存大小int size = 0;while ((size = inputStream.read(buffer)) > 0) {zos.write(buffer, 0, size);}//關閉輸入輸出流zos.closeEntry();inputStream.close();zos.finish();} catch (ZipException e) {log.error("download zipOutputStream error:{}", e.getMessage(), e);}} catch (Exception e) {log.error("download minio error:{}", e.getMessage(), e);throw new BusinessException(ErrorCodeEnum.DOWNLOAD_FAIL);} finally {if (null != zos) {try {zos.close();} catch (IOException e) {log.error(e.getMessage(), e);}}} }

小結

總結

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

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