How to return the image to the browser in the rest API in JAVA?

I want an image while I am in the API as localhost:8080:/getImage/app/path={imagePath}

While I click this API, it returns an image to me.

Is it possible?

Actually, I tried this, but it gives me an ERROR. Here is my code

 @GET @Path("/app") public BufferedImage getFullImage(@Context UriInfo info) throws MalformedURLException, IOException { String objectKey = info.getQueryParameters().getFirst("path"); return resizeImage(300, 300, objectKey); } public static BufferedImage resizeImage(int width, int height, String imagePath) throws MalformedURLException, IOException { BufferedImage bufferedImage = ImageIO.read(new URL(imagePath)); final Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setComposite(AlphaComposite.Src); // below three lines are for RenderingHints for better image quality at cost of // higher processing time graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.drawImage(bufferedImage, 0, 0, width, height, null); graphics2D.dispose(); System.out.println(bufferedImage.getWidth()); return bufferedImage; } 

My mistake,

 java.io.IOException: The image-based media type image/webp is not supported for writing 

Is there any way to return an image when hit with any url in java?

+5
source share
4 answers

You can use IOUtils . Here is a sample code.

 @RequestMapping(path = "/getImage/app/path/{filePath}", method = RequestMethod.GET) public void getImage(HttpServletResponse response, @PathVariable String filePath) throws IOException { File file = new File(filePath); if(file.exists()) { String contentType = "application/octet-stream"; response.setContentType(contentType); OutputStream out = response.getOutputStream(); FileInputStream in = new FileInputStream(file); // copy from in to out IOUtils.copy(in, out); out.close(); in.close(); }else { throw new FileNotFoundException(); } } 
+1
source

I did not test it because I do not have an environment on this computer, but logically it should work as follows: read it as an input stream and let your method return @ResponseBody byte []

 @GET @Path("/app") public @ResponseBody byte[] getFullImage(@Context UriInfo info) throws MalformedURLException, IOException { String objectKey = info.getQueryParameters().getFirst("path"); BufferedImage image = resizeImage(300, 300, objectKey); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", os); InputStream is = new ByteArrayInputStream(os.toByteArray()); return IOUtils.toByteArray(is); } 

UPDATE depending on @Habooltak Ana's suggestion there is no need to create an input stream, the code should look like this

 @GET @Path("/app") public @ResponseBody byte[] getFullImage(@Context UriInfo info) throws MalformedURLException, IOException { String objectKey = info.getQueryParameters().getFirst("path"); BufferedImage image = resizeImage(300, 300, objectKey); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", os); return os.toByteArray(); } 
+2
source

Just return the file object with the correct HTTP headers ( Content-Type and Content-Disposition ) will work in most cases / environments.

Pseudo code

 File result = createSomeJPEG(); /* eg RenderedImage rendImage = bufferedImage; File file = new File("filename.jpg"); ImageIO.write(rendImage, "jpg", file); */ response().setHeader("Content-Disposition", "attachment;filename=filename.jpg;"); response().setHeader("Content-Type", "image/jpeg"); return ok(result); 

See also:

+1
source

here's a simple solution:

 @GET @Path("/somePath") public void getImage(@Context HttpServletResponse res) throws IOException { java.nio.file.Path path = Paths.get("filePath"); res.getOutputStream().write(Files.readAllBytes(path)); res.getOutputStream().flush(); } 
0
source

Source: https://habr.com/ru/post/1275941/


All Articles