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());
嘗試使用這個。
添加回答
舉報