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

SpringBoot實現文件壓縮處理詳解

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

前言

在工作我們經常會出現有多個文件,為了節(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è)務方法實現類-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實現文件壓縮處理詳解的文章就介紹到這了,更多相關SpringBoot文件壓縮內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

通榆县| 延川县| 渭源县| 扶余县| 宿州市| 霸州市| 富民县| 阿拉善盟| 资源县| 龙游县| 卢湾区| 双江| 绵竹市| 利辛县| 三河市| 布拖县| 淳化县| 石嘴山市| 乐清市| 怀柔区| 阳谷县| 松原市| 蒙阴县| 昭苏县| 裕民县| 临澧县| 洮南市| 肇源县| 宣恩县| 互助| 大安市| 偃师市| 祁门县| 海阳市| 综艺| 志丹县| 巢湖市| 元朗区| 卢湾区| 拉萨市| 霍林郭勒市|