我需要从第三方 API 下载文件。
我在我的应用程序中编写了一个 API,它调用第三方 API 并下载文件。我能够下载文件并成功解压缩。当我点击我的 API 时,这些文件被下载到部署代码的 tomcat 服务器中。
但我想将这些文件下载到我正在执行 API 的系统中。假设,如果我将该代码部署到测试环境服务器并使用来自本地系统的 curl 命令执行我的 API,那么文件应该被下载到我的本地系统中。无论如何我可以在Java中实现这一点吗?
public class SnapshotFilesServiceImplCopy {
public static final ILogger LOGGER = FWLogFactory.getLogger();
private RestTemplate mxRestTemplate = new RestTemplate();
public void listSnapShotFiles(String diId, String snapshotGuid) {
LOGGER.debug("Entry - SnapshotFilesServiceImpl: FI=" + diId + " snapshotGuid=" + snapshotGuid);
ResponseEntity responseEntity = null;
HttpEntity entity = new HttpEntity(CommonUtil.getReportingAPIHeaders());
String resourceURL = "files_url";
try {
responseEntity = mxRestTemplate.exchange(resourceURL, HttpMethod.GET, entity, String.class);
} catch (RestClientException re) {
if (re instanceof HttpStatusCodeException) {
//TO be handled
}
}
String data = (String) responseEntity.getBody();
try {
Object obj = new JSONParser().parse(data);
JSONObject jsonObject = (JSONObject) obj;
JSONArray jsonArray = (JSONArray) jsonObject.get("accounts");
for (int i = 0; i < jsonArray.size(); i++) {
String accountFileURL = (String) jsonArray.get(i);
downloadAccountsData(diId, accountFileURL);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
鸿蒙传说
MYYA
相关分类