背景
最近被安排了一个活,纯体力的重复性工作,将开发一个项目的指定资源通过现有的下载接口下载下来。
思路
因为没有提供批量下载接口,同时下载的资源需要自己筛选,想着这样人工处理特别麻烦,个人也没有什么进步,就想着写一段代码处理,这样处理起来准确,效率高,后续有类似的任务还可以将这段代码改改后继续使用。
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
出现这种报错是必须指定的具体的文件,而不是文件夹,同时尽量保证存放文件的文件夹必须存在