在使用Spring的REST API显示图像时出现黑色背景。

huangapple go评论82阅读模式
英文:

Black background when display an image in a REST API with Spring

问题

@RequestMapping(value = "/image-manual-response", method = RequestMethod.GET)
public void getImageAsByteArray(HttpServletResponse response) throws IOException {
    InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
    IOUtils.copy(in, response.getOutputStream());
}

@GetMapping("/image")
public void getImageAsByteArray(HttpServletResponse response) throws IOException {
    InputStream in = new ByteArrayInputStream(("C:\\Users\\vartanyan\\Desktop\\images\\Puer").getBytes());
    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
    IOUtils.copy(in, response.getOutputStream());
}

在Swagger中,显示如下图所示:

在使用Spring的REST API显示图像时出现黑色背景。

当我在单独的窗口中打开图像时,显示如下图所示:

在使用Spring的REST API显示图像时出现黑色背景。

如何纠正这个问题?我正在使用Spring Boot、Hibernate和PostgreSQL编写Rest MVC应用程序。

英文:

I want to return a link to the image (or the image itself) when making a GET-request. I saw the tutorial from Baeldung and decided to use it. The code looks like this:

@RequestMapping(value = "/image-manual-response", method = RequestMethod.GET)
public void getImageAsByteArray(HttpServletResponse response) throws IOException {
    InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
    IOUtils.copy(in, response.getOutputStream());
}

Since I could not figure out what servletContext is and find the information I needed, I slightly changed the method:

    @GetMapping("/image")
    public void getImageAsByteArray(HttpServletResponse response) throws IOException {

        InputStream in = new ByteArrayInputStream(("C:\\Users\\vartanyan\\Desktop\\images\\Puer").getBytes());
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
    }

As a result, in Swagger I got the following:

在使用Spring的REST API显示图像时出现黑色背景。

And when I open the image in a separate window, I get the following:
在使用Spring的REST API显示图像时出现黑色背景。

How can this problem be corrected? I am writing Rest MVC app using Spring Boot, Hibernate, PostgreSQL.

答案1

得分: 0

从请求中获取servletContext,就像这样:

@GetMapping("/image")
public void getImageAsByteArray(HttpServletRequest request, HttpServletResponse response) throws IOException {
    InputStream in = request.getServletContext().getResourceAsStream("images/Puer.jpg");
    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
    IOUtils.copy(in, response.getOutputStream());
}
英文:

Get the servletContext from the request, like this:

@GetMapping("/image")
public void getImageAsByteArray(HttpServletRequest request, HttpServletResponse response) throws IOException {
    InputStream in = request.getServletContext().getResourceAsStream("images/Puer.jpg");
    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
    IOUtils.copy(in, response.getOutputStream());
}

答案2

得分: 0

@GetMapping("/image")
public void getImageAsByteArray(HttpServletRequest request, HttpServletResponse response) throws IOException {
    InputStream in = request.getServletContext().getResourceAsStream("C:\\Users\\vartanyan\\Desktop\\images\\Puer");
    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
    IOUtils.copy(in, response.getOutputStream());
}
英文:

Try this code:

@GetMapping("/image")
    public void getImageAsByteArray(HttpServletRequest request, HttpServletResponse response) throws IOException {
        InputStream in = request.getServletContext().getResourceAsStream("C:\\Users\\vartanyan\\Desktop\\images\\Puer");
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
    }

答案3

得分: 0

我添加了FileInputStream()的实现。例如:

public void getDrinkImage(HttpServletResponse response, Long drinkId) throws IOException {

        String imageURL = drinkRepository.getById(drinkId).getImage();

        InputStream in = new FileInputStream(uploadPath + imageURL);
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
    }
英文:

I add FileInputStream() realisation. For example:

public void getDrinkImage(HttpServletResponse response, Long drinkId) throws IOException {

        String imageURL = drinkRepository.getById(drinkId).getImage();

        InputStream in = new FileInputStream(uploadPath + imageURL);
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
    }

huangapple
  • 本文由 发表于 2020年9月4日 17:53:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63738837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定