Search code examples
javainputstreambufferedimage

How to convert BufferedImage to InputStream?


I am uploading images using servlet. To perform resize operations i am converting InputStream to BufferedImage. Now i want to save it in mongoDB. Since, i am new to mongoDB as far as i know, GridFS takes InputStream.

So, is there any way to convert BufferedImage to InputStream?


Solution

  • You need to save the BufferedImage to a ByteArrayOutputStream using the ImageIO class, then create a ByteArrayInputStream from toByteArray().