通过文件流转加密压缩文件并下载
生活随笔
收集整理的這篇文章主要介紹了
通过文件流转加密压缩文件并下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過文件流轉加密壓縮文件并下載
前言
????
代碼實現
文件流轉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);}}} }小結
總結
以上是生活随笔為你收集整理的通过文件流转加密压缩文件并下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 激光测距误差对激光脚点定位的影响
- 下一篇: 钉钉平台助力中国一汽疫情防控 数字化迈入