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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java 导出加密压缩包 设置 压缩包加密密码 并下载 (springboot+文件工具类)

發(fā)布時(shí)間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 导出加密压缩包 设置 压缩包加密密码 并下载 (springboot+文件工具类) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我們現(xiàn)在要下載一個(gè)壓縮包文件 并設(shè)置密碼,密碼統(tǒng)一固定
后臺(tái)部分:

@Value("${drap_platform.export_password}")private String exportPassword;@Overridepublic void createZip(List<TmsTemplate> list,HttpServletResponse response,HttpServletRequest request) throws IOException {// 數(shù)據(jù)準(zhǔn)備JSONObject jsonObject = new JSONObject();String jsonStr = jsonObject.toJSONString(list, SerializerFeature.WriteDateUseDateFormat);logger.debug("createZip json: {}" , jsonStr);String currTime =DateUtils.dateTimeNow();String folder = "template_" + currTime;// 將json數(shù)組寫入 template_export.json// String dirName = tarPath + "File.separator" + folder + File.separator;String dirName = tarPath + File.separator + folder + File.separator;logger.debug("要操作的文件夾: {}" , dirName);boolean exist = FileUtil.exist(dirName);if (exist) {FileUtil.del(dirName);}File jsonFile = FileUtil.file(dirName + "template_export.json");FileUtil.writeBytes(jsonStr.getBytes(),jsonFile);// 創(chuàng)建etl文件夾FileUtil.mkdir(dirName+"etl");// 遍歷所有的for (TmsTemplate tmsTemplate : list) {if ("1".equals(tmsTemplate.getTemplateKind().toUpperCase(Locale.ROOT))) {// 創(chuàng)建ETL 文件夾存放的目錄FileUtil.mkdir(dirName+"etl"+ File.separator + tmsTemplate.getTemplateId());// 根據(jù)ID 獲取 文件存放地址 , 將文件copy 到 當(dāng)前文件夾TmsTemplateFile tmsTemplateFile = new TmsTemplateFile();tmsTemplateFile.setTemplateId(tmsTemplate.getTemplateId());List<TmsTemplateFile> tmsTemplateFiles = tmsTemplateFileMapper.selectTmsTemplateFileList(tmsTemplateFile);for (TmsTemplateFile templateFile : tmsTemplateFiles) {// 復(fù)制 文件 /profile/upload/2022/08/10/c34363f17b6fa728ad720f30a59fdfa7.docx --> /upload/2022/08/10/c34363f17b6fa728ad720f30a59fdfa7.docxString fileName = templateFile.getFileUrl().substring(8);logger.debug("source file {}" , fileName);//String src = tarPath +fileName;String dest = dirName+"etl"+ File.separator + tmsTemplate.getTemplateId() + File.separator +templateFile.getFileName();logger.debug("開始復(fù)制文件 從: {} 到 {}" , src, dest);FileUtil.copy(src, dest, true);}}}// 對(duì)整個(gè)文件夾 進(jìn)行壓縮, 并且加密 dirNamelogger.debug("開始對(duì)文件夾:{} 進(jìn)行壓縮加密", dirName);String zipFileName = tarPath + File.separator + folder + ".zip";logger.debug("壓縮的以后的文件:{}", zipFileName);ZipUtil.zipEncrypt(dirName,zipFileName,exportPassword);// 修改后綴名String newFileName = tarPath + File.separator + folder + ".tms";File newFile = new File(zipFileName);FileUtil.rename(newFile,newFileName,true);// 刪除文件夾FileUtil.del(new File(dirName));// 將tms 下載到瀏覽器response.setCharacterEncoding("utf-8");response.setContentType("multipart/form-data");response.setHeader("Content-Disposition", "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, newFileName));FileUtils.writeBytes(newFileName, response.getOutputStream());// 刪除文件 刪除zipFileUtil.del(new File(newFileName));}

前端vue部分:

生成一個(gè)tmsdownload.js 去調(diào)用里面的下載 download 方法

import { downLoad } from '@/utils/tmsdownload'/** 下載模板操作 */importTemplate() {importTemplate().then(response => {this.download(response.msg)})},

tmsdownload.js :

import axios from 'axios' import { getToken } from '@/utils/auth'const mimeMap = {xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',zip: 'application/zip' }const baseUrl = window._CONFIG['BASE_URL'] export function downLoad(str, filename) {var url = baseUrl + straxios({method: 'get',url: url,responseType: 'blob',headers: { 'Authorization': 'Bearer ' + getToken() }}).then(res => {resolveBlob(res, mimeMap.zip)}) } /*** 解析blob響應(yīng)內(nèi)容并下載* @param {*} res blob響應(yīng)內(nèi)容* @param {String} mimeType MIME類型*/ export function resolveBlob(res, mimeType) {const aLink = document.createElement('a')var blob = new Blob([res.data], { type: mimeType })var fileName = 'template_' + Date.now() + '.tms'fileName = fileName.replace(/\"/g, '')aLink.href = URL.createObjectURL(blob)aLink.setAttribute('download', fileName) // 設(shè)置下載文件名稱document.body.appendChild(aLink)aLink.click()document.body.appendChild(aLink) }

總結(jié)

以上是生活随笔為你收集整理的java 导出加密压缩包 设置 压缩包加密密码 并下载 (springboot+文件工具类)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。