java 获取远程PDF文件并批量下载


package pdf;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @date 2022/10/30
 **/
@Slf4j
public class ZipDownFile {

    /***
     * 获取远程文件信息
     * @param httpUrls
     * @return {@link List< Map< String, Object>>}
     * @date 2022/10/30
    **/

    public static List<Map<String, Object>> getRomteFileData(List<String> httpUrls) throws IOException {
        String strTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        // 待下载文件列表
        List<Map<String, Object>> files = new ArrayList<>();
        // 排除重名文件
        Set<String> existFileName = new HashSet<>();
        for (String httpUrl : httpUrls) {
            String fileName = null;
            Map<String, Object> map = new HashMap<>();
            InputStream inputStream = null;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                URL url = new URL(httpUrl);
                // 获取中文文件名
                String fileNameEncode = org.apache.commons.lang3.StringUtils.substringAfter(url.getFile(), "&fileName=");
                fileName = URLDecoder.decode(fileNameEncode, CharsetUtil.UTF_8);
                if (!existFileName.add(fileName)) {
                    continue;
                }
                map.put("fileName", strTime + fileName + ".pdf");

                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                con.setConnectTimeout(10 * 1000);
                inputStream = con.getInputStream();
                if (Objects.nonNull(inputStream)) {
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buf)) > 0) {
                        bos.write(buf, 0, len);
                    }
                }
            } catch (Exception e) {
                bos.write((fileName + e.getMessage()).getBytes(StandardCharsets.UTF_8));
                map.put("fileName", strTime + fileName + ".txt");
                log.error("获取远程文件信息错误信息:" + e.getMessage());
            } finally {
                IoUtil.close(inputStream);
                IoUtil.close(bos);
            }
            map.put("outByte", bos.toByteArray());
            files.add(map);
        }
        return files;
    }

    /***
     * 批量下载文件
     * @param response 
     * @param maps 
     * @date 2022/10/30 
    **/
    
    public static void downloadZip(HttpServletResponse response, List<Map<String, Object>> maps) throws IOException {
        String strTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(strTime + "对账单", "UTF-8") + ".zip");
        ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
        try {
            for (Map<String, Object> map : maps) {
                ZipEntry zipEntry = new ZipEntry((String) map.get("fileName"));
                zipOutputStream.putNextEntry(zipEntry);
                zipOutputStream.write((byte[]) map.get("outByte"));
            }
        } catch (Exception e) {
            log.error("下载远程文件信息错误信息:" + e.getMessage());
        } finally {
            IoUtil.close(zipOutputStream);
        }
    }

    /***
     * 批量保存文件
     * @param outputStream 
     * @param maps 
     * @date 2022/10/30 
    **/
    
    public static void downloadZip(OutputStream outputStream, List<Map<String, Object>> maps) throws IOException {
        String strTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
        try {
            for (Map<String, Object> map : maps) {
                ZipEntry zipEntry = new ZipEntry((String) map.get("fileName"));
                zipOutputStream.putNextEntry(zipEntry);
                zipOutputStream.write((byte[]) map.get("outByte"));
            }
        } catch (Exception e) {
            log.error("下载远程文件信息错误信息:" + e.getMessage());
        } finally {
            IoUtil.close(zipOutputStream);
        }
    }

}

    @Test
    public void test02() throws Exception {
        List<String> httpUrls = new ArrayList<>();
        httpUrls.add("https://tsign.49icloud.com/rest/file-system/operation/download?fileName=对账单1");
        httpUrls.add("https://tsign.49icloud.com/rest/file-system/operation/download?fileName=对账单2");
        List<Map<String, Object>> romteFileData = ZipDownFile.getRomteFileData(httpUrls);

        String targetFile = "D:\\reloadD\\www\\java\\study\\_2020\\src\\main\\java\\pdf\\downFile.zip";


        ZipDownFile.downloadZip(new FileOutputStream(targetFile), romteFileData);
    }

posted on   何苦->  阅读(617)  评论(1编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示