java批量下载网络文件输出成zip包

java批量下载网络文件输出成zip包

直接上代码

	@PostMapping("/batchDownload")
    @ApiOperation("网络批量下载")
    public Result batchDownload(HttpServletResponse response, @RequestBody List<DownloadDto> strs) throws Exception {
        List<DownloadDto> certificates = strs;
        if (certificates==null){
            throw new RenException("请先选择文件,在下载!");
        }
        //组装DownloadDto省略
        String nTime = System.currentTimeMillis()+""; //获取当前时间戳
        String filename = nTime + ".zip";
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(bos);
        int l = 0;
        //循环下载
        for (DownloadDto certificate: certificates) {
            l+=1;
            zos.putNextEntry(new ZipEntry(l + certificate.getFileName()));
            String url = certificate.getUrlPath();
            byte[] bytes = downloadUrlToByte(url);
            zos.write(bytes, 0, bytes.length);
            zos.closeEntry();
        }
        zos.close();
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + filename);// 设置文件名
        File saveDri = new File(downloadFilePath);
        if (!saveDri.exists()){
            saveDri.mkdirs();
        }
       // 保存到的文件路径
        File files = new File(downloadFilePath + filename);
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(files));
        out.write(bos.toByteArray());
        out.close();
        return new Result();
    }

    public byte[] readInputStream(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = -1;
        try {
            while ((length = is.read(buffer)) != -1) {
                baos.write(buffer, 0, length);
            }
            baos.flush();
        } catch (IOException e) {
            e.printStackTrace();
//            log.error("readInputStream IOException", e);
        }
        byte[] data = baos.toByteArray();
        try {
            is.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
//            log.error("IOException", e);
        }
        return data;
    }

    /**
     * 根据文件链接把文件下载下来并且转成字节码
     */
    public byte[] downloadUrlToByte(String downloadUrl) {
        byte[] data = null;
        InputStream is = null;
        HttpURLConnection conn = null;
        try {
            URL url = new URL(downloadUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            // conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(6000);
            is = conn.getInputStream();
            if (conn.getResponseCode() == 200) {
                data = readInputStream(is);
            } else {
                data = null;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
//            log.error("downloadUrlToByte MalformedURLException", e);
        } catch (IOException e) {
            e.printStackTrace();
//            log.error("downloadUrlToByte IOException", e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
//                log.error("IOException", e);
            }
            conn.disconnect();
        }
        return data;
    }

版权声明:本文为weixin_42310713原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。