如何返回外部域中的文件?(springboot/java)

如何返回外部域中的文件?(springboot/java),java,spring-boot,Java,Spring Boot,我正在创建接收文件下载请求的SpringBootWeb服务 此服务的域是a.com,此服务从b.com域中获取请求的文件 此服务需要从外部域请求文件。 我想知道在这种情况下,如何向客户端返回文件下载请求的响应 下面的代码就是我制作的代码 @RequestMapping(path = "/downloadFile", method = RequestMethod.GET) public ResponseEntity<Resource> download(String param) th

我正在创建接收文件下载请求的SpringBootWeb服务

此服务的域是
a.com
,此服务从
b.com
域中获取请求的文件

此服务需要从外部域请求文件。

我想知道在这种情况下,如何向客户端返回文件下载请求的响应

下面的代码就是我制作的代码

@RequestMapping(path = "/downloadFile", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String param) throws IOException {
    String testFileName = "https://www.google.co.jp/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
    URL url = new URL(testFileName);
    File file = new File(url.getFile());
    HttpHeaders headers = new HttpHeaders();
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");

    Path path = Paths.get(file.getAbsolutePath());
    ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));

    return ResponseEntity.ok()
    .headers(headers)
    .contentLength(file.length())
    .contentType(MediaType.parseMediaType("application/octet-stream"))
    .body(resource);
}

尝试使用此选项。

您需要使用HTTP客户端下载要代理的文件。下面是一些示例,说明如何做到这一点。本例使用ApacheHTTP客户端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();
    }
}
Path path = Path.resolve(fileName).normalize();
Resource resource = new UrlResource(path.toUri());
@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();
    }
}