Open IntelliJ IDE and in the Controller section of your project inside the Controller class you need to build the Image serve method. The Image serve method looks like below and as you have to collect the Image response use @GetMapping Annotation to collect the image.
Create a directory to upload images from the system to your respective IDE; here in our case, It is IntelliJ.
Here, we created a directory as Mica Check and uploaded the file below:
Now time to get the Image from FileInput Stream and put it under the try-catch block as below
You getting an Image response and for that, you need to reflect the content type of the Image by adding HTTPSERVLET RESPONSE in the controller method parameter and the content type should be MEDIATYPE.(FILE_FORMAT)
Java
@GetMapping ( "/serve-image" ) public void serveImageHandler(HttpServletResponse response) { try { InputStream fileInputStream= new FileInputStream( "MicaCheck/Resignation__Approval.png" ); response.setContentType(MediaType.IMAGE_JPEG_VALUE); StreamUtils.copy(fileInputStream,response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } } |
StreamUtils.copy(file input stream,response.getOutputStream());
The above lines copy the file input stream to the response.getOutputStream. Now your Image Serve Controller method is ready. Now test it on the postman client by hitting URI as in the below Image.