springboot實現json文件生成,壓縮為zip文件并在瀏覽器下載

示例

@RestController
public class FileController {private static final Logger logger = LoggerFactory.getLogger(FileController.class);private static final String filePath = "/fileTemp";@Autowiredprivate ObjectMapper objectMapper;@GetMapping("/v1/file/add")public void add() {}@GetMapping("/v1/file/zip")public ResponseEntity<byte[]> zip() {File tempJSONFile, tempPngFile;String outputFileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));try {// 創建臨時文件根目錄File tempFileDirectory = new File(filePath);if (!tempFileDirectory.exists()) {boolean dirCreateFlag = tempFileDirectory.mkdirs();logger.info("臨時文件目錄創建成功: {}", dirCreateFlag);}// JSON數據Map<String, String> data = new HashMap<>();data.put("1", "1");for (int i = 0; i < 10; i++) {// json文件tempJSONFile = createJsonFile(i, "deviceId_" + i + ".json", objectMapper.writeValueAsString(data));if (tempJSONFile.exists()) {logger.info("JSON文件已創建");} else {logger.info("JSON文件未創建");}// 圖片文件 http://app.test.stesh.cn/images/zjlogo.pngImageTool.downloadImage("http://app.test.stesh.cn/images/zjlogo.png", filePath + "/" + i ,"deviceId_" + i + ".png");}ByteArrayOutputStream baos = new ByteArrayOutputStream();
//            ZipOutputStream zos = new ZipOutputStream(baos);
//            zipDirectory(tempFileDirectory, zos);
//            zos.close();ZipTool.createZipFile(tempFileDirectory, baos);// 刪除目錄deleteDirectory(tempFileDirectory);HttpHeaders headers = new HttpHeaders();headers.add("Content-Disposition", "attachment; filename=" + outputFileName + ".zip");return ResponseEntity.ok().headers(headers).contentLength(baos.size()).body(baos.toByteArray());} catch (IOException e) {e.printStackTrace();logger.info("error: {}", e.getMessage());} finally {}return null;}public void zipDirectory(File directory, ZipOutputStream zos) throws IOException {// zipFiles(directory, directory.getName(), zos);// 壓縮時不帶根目錄zipFiles(directory, "", zos);zos.close();}private void zipFiles(File directory, String baseName, ZipOutputStream zos) throws IOException {File[] files = directory.listFiles();byte[] buffer = new byte[1024];int length;for (File file : files) {if (file.isDirectory()) {// 如果baseName不為空,則保留除根目錄外的子目錄結構// 否則,不添加任何前綴到子目錄的zip條目中logger.info("baseName:{}", baseName);String subDirName = (baseName.isEmpty() ? "" : baseName + "/") + file.getName();zipFiles(file, subDirName, zos);continue;}// 直接使用文件名作為ZipEntry的名稱,去掉根目錄部分ZipEntry zipEntry = new ZipEntry(baseName.isEmpty() ? file.getName() : baseName + "/" + file.getName());zos.putNextEntry(zipEntry);try (FileInputStream fis = new FileInputStream(file)) {while ((length = fis.read(buffer)) > 0) {zos.write(buffer, 0, length);}}zos.closeEntry();}}public File createJsonFile(Integer i, String fileName, String jsonString) throws IOException {// 創建本地臨時文件// 創建文件目錄File tempFileDirectory = new File(filePath + "/" + i);if (!tempFileDirectory.exists()) {boolean dirCreateFlag = tempFileDirectory.mkdirs();logger.info("臨時文件目錄創建成功: {}", dirCreateFlag);}File jsonFile = new File(filePath + "/" + i + "/" + fileName);try (FileWriter fileWriter = new FileWriter(jsonFile)) {fileWriter.write(jsonString);} catch (IOException e) {e.printStackTrace();}return jsonFile;}public static void deleteDirectory(File directory) {if (directory.exists()) {File[] files = directory.listFiles();if (files != null) {for (File file : files) {if (file.isDirectory()) {// 遞歸刪除子目錄deleteDirectory(file);} else {// 刪除文件file.delete();}}}// 最后刪除目錄本身directory.delete();}}private String get(String url, Map<String, String> headers, Map<String, String> querys) {HttpClient httpClient = null;HttpGet httpGet = null;String result = null;try {httpClient = new SSLClient();URIBuilder builder = new URIBuilder(url);if (!CollectionUtils.isEmpty(querys)) {for (String key : querys.keySet()) {builder.setParameter(key, String.valueOf(querys.get(key)));}}httpGet = new HttpGet(builder.build());if (!CollectionUtils.isEmpty(headers)) {// 設置headerfor (String headerName : headers.keySet()) {httpGet.addHeader(headerName, headers.get(headerName));}}// 設置超時時間RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(1000).setSocketTimeout(10000).build();httpGet.setConfig(requestConfig);HttpResponse response = httpClient.execute(httpGet);if (response != null) {HttpEntity resEntity = response.getEntity();if (resEntity != null) {result = EntityUtils.toString(resEntity, "UTF-8");}}} catch (Exception ex) {ex.printStackTrace();}return result;}
}

http下載圖片并寫入文件

public class ImageTool {public static void downloadImage(String imageUrl, String saveFilePath, String fileName) {CloseableHttpClient httpClient = HttpClients.createDefault();HttpGet httpGet = new HttpGet(imageUrl);try {CloseableHttpResponse response = httpClient.execute(httpGet);File tempFileDirectory = new File(saveFilePath);System.out.println("path:" + tempFileDirectory.getAbsolutePath());if (!tempFileDirectory.exists()) {boolean dirCreateFlag = tempFileDirectory.mkdirs();}FileOutputStream fos = new FileOutputStream(saveFilePath + "/" + fileName);HttpEntity entity = response.getEntity();if (entity != null) {// 將圖片內容寫入文件entity.writeTo(fos);System.out.println("Image downloaded successfully!");}} catch (IOException e) {e.printStackTrace();} finally {try {if (httpClient != null) {httpClient.close();}} catch (IOException e) {e.printStackTrace();}}}
}

將文件壓縮為zip文件

public class ZipTool {public static void zipFiles(File directory, String baseName, ZipArchiveOutputStream zos) throws IOException {File[] files = directory.listFiles();if (files == null) {return; // 可能是目錄不存在或沒有讀取權限}for (File file : files) {Path filePath = file.toPath();String entryName = baseName.isEmpty() ? filePath.getFileName().toString() : baseName + "/" + filePath.getFileName().toString();if (Files.isDirectory(filePath)) {// 遞歸處理子目錄ZipArchiveEntry dirEntry = new ZipArchiveEntry(entryName + "/"); // 注意添加斜杠以表示目錄zos.putArchiveEntry(dirEntry);zos.closeArchiveEntry();zipFiles(file, entryName, zos);continue;}// 處理文件ZipArchiveEntry fileEntry = new ZipArchiveEntry(entryName);zos.putArchiveEntry(fileEntry);try (InputStream fis = Files.newInputStream(filePath)) {byte[] buffer = new byte[1024];int length;while ((length = fis.read(buffer)) > 0) {zos.write(buffer, 0, length);}}zos.closeArchiveEntry();}}// 示例方法,用于創建ZIP文件并調用zipFiles方法public static void createZipFile(File directory, ByteArrayOutputStream byteArrayOutputStream) {ZipArchiveOutputStream zos = null;try {zos = new ZipArchiveOutputStream(byteArrayOutputStream);// 假設我們不想在ZIP中包含根目錄本身zipFiles(directory, "", zos);} catch (IOException e){e.printStackTrace();} finally {try {if (zos != null) {zos.close();}} catch (IOException e) {e.printStackTrace();}}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/35742.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/35742.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/35742.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

測試基礎16:測試用例設計方法-測試大綱法

課程大綱 1、應用場景 驗證頁面跳轉&#xff1a;有多個窗口/頁面&#xff0c;每個窗口/頁面有多個動作&#xff0c;每個動作之間有相互的聯系的場景。看點擊后&#xff0c;頁面跳轉正確與否。 2、設計步驟 step1.列出大綱&#xff1a;列出涉及的頁面和頁面可執行的動作。 s…

生命在于學習——Python人工智能原理(4.7)

四、Python的程序結構與函數 4.4 函數 函數能將代碼劃分為若干模塊&#xff0c;每一個模塊可以相對獨立的實現某一個功能&#xff0c;函數有兩個主要功能&#xff0c;分別是降低編程難度和實現代碼復用&#xff0c;函數是一種功能抽象&#xff0c;復用它可以將一個復雜的大問…

深拷貝與淺拷貝的理解

深拷貝和淺拷貝是復制對象時的兩種不同方式&#xff0c;它們之間的區別在于它們如何處理對象的引用類型成員。 淺拷貝&#xff08;Shallow Copy&#xff09; 淺拷貝是指創建一個新對象&#xff0c;這個新對象是對原對象的字段的一個精確副本。對于字段中的基本數據類型&#…

【C#】找不到屬性集方法。get只讀屬性用了反射設置setValue肯定報錯

歡迎來到《小5講堂》 這是《C#》系列文章&#xff0c;每篇文章將以博主理解的角度展開講解。 溫馨提示&#xff1a;博主能力有限&#xff0c;理解水平有限&#xff0c;若有不對之處望指正&#xff01; 背景 找不到屬性集方法。get只讀屬性用了反射設置setValue肯定報錯 報錯…

IPFoxy Tips:匿名海外代理IP的使用方法及注意事項

在互聯網上&#xff0c;隱私和安全問題一直備受關注。為了保護個人隱私和數據安全&#xff0c;使用匿名代理IP是一種常用的方法。匿名代理IP可以隱藏用戶的真實IP地址&#xff0c;使用戶在訪問網站時更加隱秘和安全。 本文將介紹匿名代理IP的基本原理和核心功能。 基本原則 匿…

2024期權交易傭金手續費最低是多少?期權交易有哪些成本?

顯性成本 期權交易的顯性成本包含期權交易的傭金和交易所費用&#xff0c;分別支付給券商和交易所&#xff0c;統一由券商代收。 傭金 期權傭金是期權交易時支付給券商的費用&#xff0c;傭金通常以交易金額的一定比例計算&#xff0c;可以是固定費用&#xff0c;也可以是滑…

用兩個鐘,我又在VMWARE上搞了一套內部網配置

最近要學es&#xff0c;所以打算自己用虛擬機搞個NAT&#xff0c;又搞了兩個鐘。為了不再費勁嘗試&#xff0c;也為了造福大眾&#xff0c;所以選擇搞一份NAT筆記&#xff01;&#xff01;&#xff01;&#xff01; 1.初始化網關和DNS 我們給網關配置一個地址192.168.96.1&…

【多模態】BEiT v2

鏈接&#xff1a;https://arxiv.org/pdf/2208.06366 論文&#xff1a;BEIT V2: Masked Image Modeling with Vector-Quantized Visual Tokenizers Introduction Motivation&#xff1a;Masked image modeling (MIM) 任務在自監督表征學習上取得了不錯的成績&#xff0c;但是現…

教師資格證考試面試報名流程

文章目錄 前言面試報名流程一、登錄官網二、選擇報考省份三、注冊報名賬號四、確認考試承諾五、填報個人信息六、上傳個人照片七、查看個人信息八、面試報名九、等待審核十、考試繳費最后&#xff08;必看&#xff09;附錄1. 中小學教師資格考試網2. 廣東省教資考試報名通知&am…

Linux加固-權限管理_chattr之i和a參數

一、參數i i:如果對文件設置了i屬性&#xff0c;不允許對文件進行刪除、改名&#xff0c;也不能添加和修改數據&#xff1b;如果對目錄設置了i屬性&#xff0c;那么只能修改目錄下文件的數據&#xff0c;但不允許建立和刪除文件。&#xff08;相當于把文件給鎖住了&#xff0c;…

【Mysql】多表、外鍵約束

多表 1.1 多表簡述 實際開發中&#xff0c;一個項目通常需要很多張表才能完成。 例如一個商城項目的數據庫,需要有很多張表&#xff1a;用戶表、分類表、商品表、訂單表… 1.2 單表的缺點 1.2.1 數據準備 創建一個數據庫 db3 CREATE DATABASE db3 CHARACTER SET utf8;數據庫…

Segment any Text:優質文本分割是高質量RAG的必由之路

AI應用開發相關目錄 本專欄包括AI應用開發相關內容分享&#xff0c;包括不限于AI算法部署實施細節、AI應用后端分析服務相關概念及開發技巧、AI應用后端應用服務相關概念及開發技巧、AI應用前端實現路徑及開發技巧 適用于具備一定算法及Python使用基礎的人群 AI應用開發流程概…

基于qemu_v8 + optee400構建自定義app

構建基于libckteec的tls安全通信應用程序&#xff0c;應用目錄結構 $ tree -L 2 . ├── libp11 │ ├── libp11-libp11-0.4.12 │ ├── mk_optee_three_part.sh │ └── out ├── openssl │ ├── mk_optee_three_part.sh │ ├── openssl-1.1.1w │ …

vue項目中封裝element分頁組件

我們都知道封裝組件是為了方便在項目中使用&#xff0c;全局封裝之后哪個模塊使用直接復制就行了&#xff0c;分頁在后臺項目中用到的地方也是很多的&#xff0c;所以我們就全局封裝一下分頁組件&#xff0c;以后也方便在項目中使用&#xff0c;接下來封裝的這個分頁也是elemen…

clean code-代碼整潔之道 閱讀筆記(第十四章)

第十四章 逐步改進——對一個命令行參數解析程序的案例研究 ps&#xff1a;本章設計代碼示例所以篇幅會較長&#xff0c;推薦直接看原文&#xff0c;思路、代碼講解的很清楚 本章示例&#xff1a;解析命令行參數的工具 —— Args Args的簡單用法 public static void main(Stri…

vue中動態綁定樣式名的方式有幾種?

在Vue中可以使用動態綁定樣式名的方式有幾種,具體取決于你的需求和使用的場景。 使用對象語法: 可以通過在data中定義一個變量,然后在模板中使用對象語法來動態綁定樣式名。 <template><div :class="{ active: isActive }">Hello Vue!</div> &l…

網絡文化經營許可證(文網文)辦理全面講解

隨著互聯網時代的飛速發展&#xff0c;互聯網早已滲透到人們的生活中&#xff0c;各類直播、短視頻成為大家生活娛樂必不可少的一部分。注冊一家從事互聯網行業的企業是一個不錯的選擇。那互聯網企業需要辦理什么證件資質呢&#xff1f;在互聯網行業從事盈利文化活動必須持有網…

【精品方案】智能制造之路(93頁PPT)

引言&#xff1a;智能制造之路&#xff1a;革新制造業的引領之旅 隨著科技的迅猛發展&#xff0c;特別是人工智能、物聯網、大數據等技術的不斷進步&#xff0c;制造業正迎來一場深刻的變革。智能制造&#xff0c;作為這場變革的核心&#xff0c;正逐步成為推動產業升級和轉型發…

MySQL為什么不建議使用多表JOIN

一、典型回答 之所以不建議使用JOIN查詢&#xff0c;最主要的原因就是JOIN的效率比較低。 MySQL是使用了嵌套循環&#xff08;Nested-Loop Join&#xff09;的方式實現關聯查詢的&#xff0c;簡單點說就是要通過兩層循環&#xff0c;用第一張表做外循環&#xff0c;第二張表做內…

大模型課程資料-全網最火29套全棧大模型項目實踐

29套AI全棧大模型項目實戰&#xff0c;人工智能視頻課程-多模態大模型&#xff0c;微調技術訓練營&#xff0c;大模型多場景實戰&#xff0c;AI圖像處理&#xff0c;AI量化投資&#xff0c;OPenCV視覺處理&#xff0c;機器學習&#xff0c;Pytorch深度學習&#xff0c;推薦系統…