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

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

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值