英文:
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 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:
And when I open the image in a separate window, I get the following:
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论