2 回答
TA贡献1810条经验 获得超4个赞
您需要使用 HTTP 客户端下载您要代理的文件。下面是一些如何做到这一点的示例。此示例使用 Apache HTTP 客户端 4.5。
@RequestMapping(path = "/downloadFile", method = RequestMethod.GET)
public ResponseEntity download(String param) throws IOException {
String testFileName = "https://www.google.co.jp/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(testFileName);
request.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
request.setHeader("Pragma", "no-cache");
request.setHeader("Expires", "0");
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode()==200) {
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));
return ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(response.getEntity().getContent());
} else {
return ResponseEntity.notFound();
}
}
TA贡献1847条经验 获得超11个赞
Path path = Path.resolve(fileName).normalize(); Resource resource = new UrlResource(path.toUri());
尝试使用这个。
添加回答
举报