在具有 Spring 的 REST API 中显示图像时的黑色背景
Black background when display an image in a REST API with Spring
我想在发出 GET
请求时 return 对图像(或图像本身)进行 link。我看到了 Baeldung 的教程并决定使用它。代码如下所示:
@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());
}
由于一直搞不清楚servletContext
是什么,也找不到自己需要的资料,所以稍微改了一下方法:
@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 中我得到了以下信息:
当我在单独的 window 中打开图像时,我得到以下信息:
如何解决这个问题?我正在使用 Spring Boot
、Hibernate
、PostgreSQL
.
编写 Rest
MVC
应用程序
从请求中获取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());
}
试试这个代码:
@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());
}
我添加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());
}
我想在发出 GET
请求时 return 对图像(或图像本身)进行 link。我看到了 Baeldung 的教程并决定使用它。代码如下所示:
@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());
}
由于一直搞不清楚servletContext
是什么,也找不到自己需要的资料,所以稍微改了一下方法:
@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 中我得到了以下信息:
当我在单独的 window 中打开图像时,我得到以下信息:
如何解决这个问题?我正在使用 Spring Boot
、Hibernate
、PostgreSQL
.
Rest
MVC
应用程序
从请求中获取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());
}
试试这个代码:
@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());
}
我添加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());
}