这个工具类知道了已经要压缩的文件的路径,然后需要将这个路径下的文件进行压缩。

    /**
     * 压缩下载照片
     *
     * @param picUrl
     * @param response
     * @throws IOException
     */
    public static void downloadPic(List<String> picUrl, HttpServletResponse response) throws IOException, AdminException {
        try {
            String downloadFilename = System.currentTimeMillis() + ".zip";//文件的名称
            downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
            response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
            response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            String[] files = new String[picUrl.size()];
            picUrl.toArray(files);
            for (int i = 0; i < files.length; i++) {
                String url = files[i];
                zos.putNextEntry(new ZipEntry(downloadFilename + File.separator + i + ".jpg"));
                InputStream fis = new FileInputStream(new File(url));
                byte[] buffer = new byte[1024];
                int r = 0;
                while ((r = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, r);
                }
                fis.close();
            }
            zos.flush();
            zos.close();
        } catch (UnsupportedEncodingException e) {
            logger.error("不支持当前格式",e);
        }
    }

 

相关文章:

  • 2022-12-23
  • 2021-11-03
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2022-02-07
猜你喜欢
  • 2022-01-09
  • 2021-12-02
  • 2021-10-30
  • 2021-11-30
  • 2022-01-14
  • 2021-11-08
  • 2022-12-23
相关资源
相似解决方案