最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot實(shí)現(xiàn)文件壓縮處理詳解

 更新時間:2024年11月14日 08:20:23   作者:[奮斗]  
在工作我們經(jīng)常會出現(xiàn)有多個文件,為了節(jié)省資源會將多個文件放在一起進(jìn)行壓縮處理,本文將使用SpringBoot實(shí)現(xiàn)文件壓縮處理,感興趣的可以了解下

前言

在工作我們經(jīng)常會出現(xiàn)有多個文件,為了節(jié)省資源會將多個文件放在一起進(jìn)行壓縮處理;為了讓大家進(jìn)一步了解我先將springboot處理的方法總結(jié)如下,有不到之處敬請大家批評指正!

一、文件準(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)建一個springboot web項目 這一步在此省略.....

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)對象
     */
    @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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

宜君县| 隆德县| 长宁区| 清流县| 贡山| 宁都县| 福州市| 开阳县| 昔阳县| 扶余县| 叶城县| 枝江市| 洛阳市| 吉林省| 肥城市| 盘锦市| 如皋市| 沛县| 巢湖市| 涟源市| 灵川县| 武平县| 信阳市| 布拖县| 泸州市| 共和县| 东明县| 吉安县| 阿鲁科尔沁旗| 九江县| 祁阳县| 扶风县| 志丹县| 祁东县| 广东省| 龙南县| 连山| 万全县| 页游| 西畴县| 肃宁县|