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

大恶魔

我正在制作接收文件下载请求的 Spring Boot Web 服务。

此服务的域是,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);
}

但由于文件路径,它不起作用。它的路径是这样显示的。

/Users/user/devel/acomapplication/https:/www.google.co.jp/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png
斯文

您需要使用 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();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何用黄瓜激活弹簧靴配置文件

来自分类Dev

带有外部调度程序的骆驼弹簧靴

来自分类Dev

如何从外部Java脚本文件访问Java Scriplet变量?

来自分类Dev

如何设置 Java ProcessBuilder 参数以运行外部 .java 文件?

来自分类Dev

如何返回文件java中的行

来自分类Dev

Java:如何从内部类中的方法返回外部方法

来自分类Dev

部署弹簧靴胖子

来自分类Dev

Swagger 的弹簧靴骨架

来自分类Dev

从弹簧靴开始

来自分类Dev

Hazelcast 带弹簧靴

来自分类Dev

带 mockito 的弹簧靴

来自分类Dev

如何在Java中检索外部.properties文件?

来自分类Dev

Java同步外部文件读写

来自分类Dev

如何获得弹簧启动@Async工作与Java 8

来自分类Dev

如何使用Java中的REST API返回zip文件?

来自分类Dev

如何处理Java中从JFileChooser返回的文件

来自分类Dev

如何从该.java文件(Java)外部实例化嵌套在类内部的类?

来自分类Dev

如何从.java文件(Java)外部实例化嵌套在类内部的类?

来自分类Dev

将弹簧靴与RESTEasy集成

来自分类Dev

如何读取位于工作区外部文件夹中的java类中的属性文件?

来自分类Dev

如何从Gradle外部库(JAVA)的文件夹中加载文件

来自分类Dev

忽略安装的java版本如何返回java.exe文件的完整路径?

来自分类Dev

使用Java从外部库修改文件

来自分类Dev

在Java Android中加载外部JSON文件

来自分类Dev

读取外部属性文件 Java/Linux

来自分类Dev

使我的 Java 程序与外部类文件交互

来自分类Dev

从Java的代码顶部返回,如何?

来自分类Dev

Java。如何返回广义对象?

来自分类Dev

如何从Eclipse编译的命令行中运行Java类文件?外部Jar问题

Related 相关文章

热门标签

归档