SpringBoot實現(xiàn)文件壓縮處理詳解
前言
在工作我們經常會出現(xiàn)有多個文件,為了節(jié)省資源會將多個文件放在一起進行壓縮處理;為了讓大家進一步了解我先將springboot處理的方法總結如下,有不到之處敬請大家批評指正!
一、文件準備
https://qnsc.oss-cn-beijing.aliyuncs.com/crmebimage/public/product/2024/11/12/be353210028a3da732c8ba34073fb4ca.jpeg https://qnsc.oss-cn-beijing.aliyuncs.com/crmebimage/public/product/2024/11/13/5bbf579109db2641249deab4be4340f6.jpeg https://qnsc.oss-cn-beijing.aliyuncs.com/crmebimage/public/product/2024/11/13/1808773678128361474.xlsx
二、處理步驟
1.創(chuàng)建一個springboot web項目 這一步在此省略.....
2.需要的方法及類的編寫
(1)業(yè)務方法-TestService
public interface TestService { void compressFiles(List<String> fileUrls, HttpServletResponse response); }
(2)業(yè)務方法實現(xiàn)類-TestServiceImpl
@Service @Slf4j public class TestServiceImpl implements TestService { @Override public void compressFiles(List<String> fileUrls, HttpServletResponse response) { try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) { for (String fileUrl : fileUrls) { // 1.從網絡下載文件并寫入 ZIP try { URL url = new URL(fileUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); // 2.檢查響應碼 if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new IOException("Failed to download file: " + fileUrl); } // 3.從 URL 中提取文件名 String pathStr = fileUrl.substring(fileUrl.lastIndexOf('/') + 1); // 4.創(chuàng)建 ZIP 條目 ZipEntry zipEntry = new ZipEntry(pathStr); zipOut.putNextEntry(zipEntry); // 5.讀取文件的輸入流 try (InputStream inputStream = new BufferedInputStream(connection.getInputStream())) { byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) >= 0) { zipOut.write(buffer, 0, length); } } zipOut.closeEntry(); } catch (IOException e) { log.error("Error processing file URL: " + fileUrl, e); throw new RuntimeException(e); } } // 6.響應信息設置處理 response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=test.zip"); response.flushBuffer(); } catch (IOException e) { log.error("Error compressing files", e); throw new RuntimeException(e); } } }
(3)控制器類的編寫-TestController
/** * @Project: * @Description: * @author: songwp * @Date: 2024/11/13 14:50 **/ @RequestMapping("test") @RestController @Slf4j public class TestController { @Autowired private TestService testService; /** * 文件壓縮 * * @param fileUrls 要壓縮的文件 URL 列表 * @param response 響應對象 */ @GetMapping("/fileToZip") public void zip(@RequestParam("fileUrls") List<String> fileUrls, HttpServletResponse response) { testService.compressFiles(fileUrls, response); } }
三、方法調用展示
(1)存放到桌面
(2)解壓response.zip文件
到此這篇關于SpringBoot實現(xiàn)文件壓縮處理詳解的文章就介紹到這了,更多相關SpringBoot文件壓縮內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java獲取兩個集合List的交集、補集、并集(相加)和差集(相減)的幾種方式
這篇文章主要介紹了Java獲取兩個集合List的交集、補集、并集(相加)和差集(相減)的幾種方式,文中通過代碼示例講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2025-04-04Mybatis-plus 批量插入太慢的問題解決(提升插入性能)
公司使用的Mybatis-Plus操作SQL,用過Mybatis-Plus的小伙伴一定知道他有很多API提供給我們使用,但是批量插入大數(shù)據(jù)太慢應該怎么解決,本文就詳細的介紹一下,感興趣的可以了解一下2021-11-11解決Mybatis中mapper.xml文件update,delete及insert返回值問題
這篇文章主要介紹了解決Mybatis中mapper.xml文件update,delete及insert返回值問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11SpringMVC加載控制與Postmand的使用和Rest風格的引入及RestFul開發(fā)全面詳解
SpringMVC是一種基于Java,實現(xiàn)了Web MVC設計模式,請求驅動類型的輕量級Web框架,即使用了MVC架構模式的思想,將Web層進行職責解耦?;谡埱篁寗又傅木褪鞘褂谜埱?響應模型,框架的目的就是幫助我們簡化開發(fā),SpringMVC也是要簡化我們日常Web開發(fā)2022-10-10Spring中的@PostConstruct注解使用方法解析
這篇文章主要介紹了Spring中的@PostConstruct注解使用方法解析,@PostConstruct注解是用來處理在@Autowired注入屬性后init()方法之前,對一些零散的屬性進行賦值的注解,需要的朋友可以參考下2023-11-11Java隨機值設置(java.util.Random類或Math.random方法)
在編程中有時我們需要生成一些隨機的字符串作為授權碼、驗證碼等,以確保數(shù)據(jù)的安全性和唯一性,這篇文章主要給大家介紹了關于Java隨機值設置的相關資料,主要用的是java.util.Random類或Math.random()方法,需要的朋友可以參考下2024-08-08