Java + Spring Boot:下载图像并将其传递给请求

DaU*_*ser 3 java servlets image download spring-boot

我有一个Spring Boot应用程序,它应该像代理一样工作。

它应处理类似“ http:// imageservice / picture / 123456 ”的请求

然后,应用程序应生成一个对“ http://internal-picture-db/123456.jpg ” 的新请求,在该应用程序中应下载其后的图片(123456.jpg),然后将其传递给响应并提供服务。

应该像...

@RequestMapping("/picture/{id}")
public String getArticleImage(@PathVariable String id, HttpServletResponse response) {

    logger.info("Requested picture >> " + id + " <<");

    // 1. download img from http://internal-picture-db/id.jpg ... 

    // 2. send img to response... ?!

    response.???

}
Run Code Online (Sandbox Code Playgroud)

我希望我的意思很清楚...

所以我的问题是:最好的方法是什么?

并且仅出于信息目的,不可能仅发送重定向,因为该系统在Internet中不可用。

Ste*_*eph 8

我将使用响应主体返回图像,而不是视图,例如:

@RequestMapping("/picture/{id}")
@ResponseBody
public HttpEntity<byte[]> getArticleImage(@PathVariable String id) {

    logger.info("Requested picture >> " + id + " <<");

    // 1. download img from http://internal-picture-db/id.jpg ... 
    byte[] image = ...

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    headers.setContentLength(image.length);

    return new HttpEntity<byte[]>(image, headers);
}
Run Code Online (Sandbox Code Playgroud)

您有一篇文章可以帮助您从其他URL 下载图像如何从Java中的任何网页下载图像