<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      SpringBoot-文件壓縮處理

      前言

      在工作我們經(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文件

       

      posted @ 2024-11-13 15:00  [奮斗]  閱讀(499)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 竹菊影视欧美日韩一区二区三区四区五区 | 久久久亚洲欧洲日产国码农村 | 国产一区二区三区在线观看免费 | 年轻女教师hd中字3| 欧美黑人巨大videos精品| 荡乳尤物h| 熟妇激情一区二区三区| 国产精品综合av一区二区国产馆| 欧美高清狂热视频60一70| 无码国产精品一区二区免费虚拟vr| 日本九州不卡久久精品一区| 国产老熟女无套内射不卡| 久久久久人妻精品一区三寸 | 国产SM重味一区二区三区| 亚洲欧美综合在线天堂| 江门市| 久久碰国产一区二区三区| 国产久免费热视频在线观看| 沐川县| 国产无遮挡猛进猛出免费软件| 蜜桃视频一区二区在线观看| 泰顺县| 麻花传媒在线观看免费| 亚洲国产一区二区三区亚瑟| 久久亚洲色www成人欧美| 亚欧洲乱码视频在线专区| 欧美孕妇乳喷奶水在线观看| 蓬莱市| 亚洲成年av天堂动漫网站| 久久久成人毛片无码| 人妻av无码系列一区二区三区| 中文人妻AV高清一区二区| 六月丁香婷婷色狠狠久久| 久久99热只有频精品8| 亚洲国产一区二区三区| 97se亚洲综合在线天天| 亚洲一区二区中文字幕| 亚洲国产成人久久综合人| 香蕉在线精品一区二区| 亚洲三级香港三级久久| 蜜臀视频在线观看一区二区|