SpringBoot 使用RestTemplate通过http请求讲文件下载到本地

背景

最近被安排了一个活,纯体力的重复性工作,将开发一个项目的指定资源通过现有的下载接口下载下来。

思路

因为没有提供批量下载接口,同时下载的资源需要自己筛选,想着这样人工处理特别麻烦,个人也没有什么进步,就想着写一段代码处理,这样处理起来准确,效率高,后续有类似的任务还可以将这段代码改改后继续使用。

1.筛选
筛选出需要的下载文件的id,这个可以根据业务调整,不一定是id,具体的实现根据业务逻辑实现;

2.下载
下载方式有两种,一种是通过postman或者apifox这种工具,通过提前构建参数和提供写有参数的文件,批量出发请求,这种只是大致想到的可以不靠编码实现的方式;另外一种是借助后端代码发起http请求,将下载的资源写入本地文件中。下面简单介绍第二种,需要时可以批量下载文件。

实现方式

资源下载方法

public static void downloadFile(String url, HttpHeaders headers, RequestParams requestParams, String outputPath) throws IOException {


        // 创建请求实体
        HttpEntity<RequestParams> entity = new HttpEntity<>(requestParams, headers);
        // 初始化RestTemplate,也可以自己构建配置类。
        final RestTemplate restTemplate = new RestTemplate();
        // 请求配置
        ResponseEntity<Resource> response = restTemplate.exchange(
                URI.create(url),
                HttpMethod.POST,
                entity,
                Resource.class
        );

        // 请求成功校验
        if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
            // 获取资源
            Resource resource = response.getBody();

            // 确保目录存在
            File outputFile = new File(outputPath);
            File parentDir = outputFile.getParentFile();
            // 文件存在校验
            if (parentDir != null && !parentDir.exists()) {
                if (!parentDir.mkdirs()) {
                    throw new IOException("Failed to create directory: " + parentDir);
                }
            }
            //获取流
            try (InputStream inputStream = resource.getInputStream();
                 OutputStream outputStream = Files.newOutputStream(outputFile.toPath())) {
                StreamUtils.copy(inputStream, outputStream);
            }
        } else {
            throw new IOException("Failed to download file: " + response.getStatusCode());
        }
    }

实体类(可以自己定义)

@Data
public class RequestParams implements Serializable {
    private Long id;
}

函数调用

    public static void downloadPerPaper(PaperInfo paperInfos) throws IOException {
		//url
        String url="https://xxxx.com";
        //请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("xxx","xxxxx");
        //请求参数
        final RequestParams requestParams = new RequestParams();
        requestParams.setId(1L);
        //存在存放地址
        String path="xx/xx/xx.doc";
        //调用下载方法
        downloadFile(url,headers,requestParams,path);
    }

异常处理

Exception in thread "main" java.nio.file.AccessDeniedException: xx/xx/xx

出现这种报错是必须指定的具体的文件,而不是文件夹,同时尽量保证存放文件的文件夹必须存在

相关推荐

  1. springboot使用RestTemplate 请求http接口

    2024-06-07 21:16:05       12 阅读
  2. RestTemplate发送https请求

    2024-06-07 21:16:05       39 阅读
  3. springboot设置RestTemplate支持http&https

    2024-06-07 21:16:05       11 阅读
  4. SpringBoot RestTemplate 上传文件

    2024-06-07 21:16:05       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 21:16:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 21:16:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 21:16:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 21:16:05       20 阅读

热门阅读

  1. Informer

    Informer

    2024-06-07 21:16:05      8 阅读
  2. 前后端交互:axios 和 json;springboot 和 vue

    2024-06-07 21:16:05       9 阅读
  3. uniapp手机屏幕左滑返回上一页支持APP,H5

    2024-06-07 21:16:05       6 阅读
  4. 08-使用HappyPack提升Webpack构建速度

    2024-06-07 21:16:05       10 阅读
  5. MATLAB 矩阵

    2024-06-07 21:16:05       6 阅读
  6. 网络安全第一课

    2024-06-07 21:16:05       5 阅读
  7. RN的安卓和iOS打包步骤(软件托管平台推荐)

    2024-06-07 21:16:05       9 阅读
  8. 开源大模型源代码

    2024-06-07 21:16:05       5 阅读
  9. 在RT-Thread下为MPU手搓以太网MAC驱动-4

    2024-06-07 21:16:05       8 阅读