SpringBoot實(shí)現(xiàn)文件壓縮處理詳解
前言
在工作我們經(jīng)常會(huì)出現(xiàn)有多個(gè)文件,為了節(jié)省資源會(huì)將多個(gè)文件放在一起進(jìn)行壓縮處理;為了讓大家進(jìn)一步了解我先將springboot處理的方法總結(jié)如下,有不到之處敬請(qǐng)大家批評(píng)指正!
一、文件準(zhǔn)備
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)建一個(gè)springboot web項(xiàng)目 這一步在此省略.....
2.需要的方法及類的編寫
(1)業(yè)務(wù)方法-TestService
public interface TestService { void compressFiles(List<String> fileUrls, HttpServletResponse response); }
(2)業(yè)務(wù)方法實(shí)現(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.從網(wǎng)絡(luò)下載文件并寫入 ZIP try { URL url = new URL(fileUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); // 2.檢查響應(yīng)碼 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.響應(yīng)信息設(shè)置處理 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 響應(yīng)對(duì)象 */ @GetMapping("/fileToZip") public void zip(@RequestParam("fileUrls") List<String> fileUrls, HttpServletResponse response) { testService.compressFiles(fileUrls, response); } }
三、方法調(diào)用展示
(1)存放到桌面
(2)解壓response.zip文件
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)文件壓縮處理詳解的文章就介紹到這了,更多相關(guān)SpringBoot文件壓縮內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA實(shí)現(xiàn)單例模式的四種方法和一些特點(diǎn)
JAVA實(shí)現(xiàn)單例模式的四種方法和一些特點(diǎn),需要的朋友可以參考一下2013-03-03java文字轉(zhuǎn)語音播報(bào)功能的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于java文字轉(zhuǎn)語音播報(bào)功能的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Java獲取兩個(gè)集合List的交集、補(bǔ)集、并集(相加)和差集(相減)的幾種方式
這篇文章主要介紹了Java獲取兩個(gè)集合List的交集、補(bǔ)集、并集(相加)和差集(相減)的幾種方式,文中通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2025-04-04Mybatis-plus 批量插入太慢的問題解決(提升插入性能)
公司使用的Mybatis-Plus操作SQL,用過Mybatis-Plus的小伙伴一定知道他有很多API提供給我們使用,但是批量插入大數(shù)據(jù)太慢應(yīng)該怎么解決,本文就詳細(xì)的介紹一下,感興趣的可以了解一下2021-11-11解決Mybatis中mapper.xml文件update,delete及insert返回值問題
這篇文章主要介紹了解決Mybatis中mapper.xml文件update,delete及insert返回值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11SpringMVC加載控制與Postmand的使用和Rest風(fēng)格的引入及RestFul開發(fā)全面詳解
SpringMVC是一種基于Java,實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式,請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)Web框架,即使用了MVC架構(gòu)模式的思想,將Web層進(jìn)行職責(zé)解耦?;谡?qǐng)求驅(qū)動(dòng)指的就是使用請(qǐng)求-響應(yīng)模型,框架的目的就是幫助我們簡(jiǎn)化開發(fā),SpringMVC也是要簡(jiǎn)化我們?nèi)粘eb開發(fā)2022-10-10Spring中的@PostConstruct注解使用方法解析
這篇文章主要介紹了Spring中的@PostConstruct注解使用方法解析,@PostConstruct注解是用來處理在@Autowired注入屬性后init()方法之前,對(duì)一些零散的屬性進(jìn)行賦值的注解,需要的朋友可以參考下2023-11-11Java隨機(jī)值設(shè)置(java.util.Random類或Math.random方法)
在編程中有時(shí)我們需要生成一些隨機(jī)的字符串作為授權(quán)碼、驗(yàn)證碼等,以確保數(shù)據(jù)的安全性和唯一性,這篇文章主要給大家介紹了關(guān)于Java隨機(jī)值設(shè)置的相關(guān)資料,主要用的是java.util.Random類或Math.random()方法,需要的朋友可以參考下2024-08-08