0

I have a java web application that at some point will render pages with a lot of pictures. Those pictures are of inserted in jsp as html img tags. User can see those in the browser.

The quantity of pictures is growing and I am thinking to move them in gridfs.

My question: is any way to select the file and present-it to client in web page without writing it on hard-drive?

I have to mention that are a lot of concurrent http request for various pages which contains img links.

Mircea Stanciu
  • 3,675
  • 3
  • 34
  • 37

1 Answers1

0

Found the answer. I can see the image in browser, therefore use a as hyperlink

@RequestMapping (value = "/test", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public byte[] test(HttpServletRequest request) {
    //curl -v http://localhost:8080/Bikeshop/admin/test > /dev/null
    String realPath = request.getSession().getServletContext().getRealPath("/resources/images/catalog/sample.jpg");

    try {
        InputStream is = new FileInputStream(realPath);
        BufferedImage img = ImageIO.read(is);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(img, "jpg", bos);
        return bos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    return null;
}
Mircea Stanciu
  • 3,675
  • 3
  • 34
  • 37