文件下载--ZipOutputStream批量&OutputStream单个文件下载图片到本地

2 篇文章 0 订阅
该博客展示了如何使用ZipOutputStream批量下载多个网络图片,并将其打包成ZIP文件,以及如何通过OutputStream单个下载图片。在批量下载中,文件按URL自定义目录保存,而在单个文件下载中,直接输出到响应流中。这两个示例对于处理HTTP响应流下载文件具有实际应用价值。
摘要由CSDN通过智能技术生成

ZipOutputStream批量&OutputStream单个文件下载图片到本地

1.使用ZipOutputStream批量打包文件到本地

demo:

@RestController
@RequestMapping("download")
@Slf4j
public class DownLoadController {

    @RequestMapping("test")
    public void test(HttpServletResponse response) {
        try {
            String nowTimeString = "ll";
            //文件的名称
            String downloadFilename = nowTimeString + ".zip";
            //转换中文否则可能会产生乱码
            downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
            // 指明response的返回对象是文件流
            response.setContentType("application/octet-stream");
            // 设置在下载框默认显示的文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            ArrayList<String> files = Lists.newArrayList(
                    "http://***/commodity_qrcode/qrcode_test/1983207019.jpg", "http://****/commodity_qrcode/qrcode_test/1979780937.jpg", "http://***/commodity_qrcode/qrcode_test/1979780935.jpg",
                    "http://***/commodity_qrcode/qrcode_test/1979780934.jpg", "http://****/commodity_qrcode/qrcode_test/1152809.jpg", "http://****/commodity_qrcode/qrcode_test/1152808.jpg",
                   "http://***/commodity_qrcode/qrcode_test/1152807.jpg",
                    "https://****/lll/test/2020-12/memo/f86dc6ba7a7642ac83f9f4409af2227e.jpg");

            for (int i = 0; i < files.size(); i++) {
                URL url = new URL(files.get(i));
                //目录不能重复,否则会duplicate entry: ll/
                //因此需要加个时间戳或者是uuid作为后缀
                //也可以根据文件地址作为目录,会自动根据路径分区文件夹,一个/一个文件目录
                zos.putNextEntry(new ZipEntry("ll/" + i+ ".jpg"));
                //此时会自动分级目录,
//                zos.putNextEntry(new ZipEntry(files.get(i));

                InputStream fis = url.openConnection().getInputStream();
                byte[] buffer = new byte[1024];
                int r = 0;
                while ((r = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, r);
                }
                fis.close();
            }

            //强制把数据输出,清空缓存区,(必须要,因为一部分数据还在缓冲区)
            //面向缓冲区的,不强制输出就关闭,数据可能不完整
            //最后再关闭读写流调用close()就完成了
            zos.flush();
            //关闭输出流
            zos.close();
        } catch (Exception e) {
            log.error("下载失败", e);

        }
    }
  }

结果: 1.根据url分目录
在这里插入图片描述
2.自定义目录
在这里插入图片描述

2.OutputStream单个文件下载图片到本地

demo

@RestController
@RequestMapping("download")
@Slf4j
public class DownLoadController {

    @RequestMapping("singleQrCode")
    public void getSingleQrCode(HttpServletResponse response, String imgUrl) {
        try {
            String downloadFilename = "new url";
            downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);
            response.setHeader("FileName", downloadFilename);
            OutputStream outputStream = response.getOutputStream();
            URL url = new URL(imgUrl);
            InputStream fis = url.openConnection().getInputStream();
            byte[] buffer = new byte[1024];
            int r;
            while ((r = fis.read(buffer)) != -1) {
                outputStream.write(buffer, 0, r);
            }
            fis.close();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            log.warn("品牌商二维码下载失败{}", JSON.toJSONString(imgUrl), e);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值