如何将BufferedImage转换为InputStream? [英] How to convert BufferedImage to InputStream?
问题描述
我正在使用servlet上传图片。要执行调整大小操作,我将InputStream转换为BufferedImage。现在我想将它保存在mongoDB中。因为,据我所知,我是mongoDB的新手,GridFS接受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.
那么,有没有办法将BufferedImage转换为InputStream?
So, is there any way to convert BufferedImage to InputStream?
推荐答案
您需要将BufferedImage保存到 ByteArrayOutputStream
使用 ImageIO
class ,然后创建一个 ByteArrayInputStream
来自 toByteArray()
。
You need to save the BufferedImage to a ByteArrayOutputStream
using the ImageIO
class, then create a ByteArrayInputStream
from toByteArray()
.
这篇关于如何将BufferedImage转换为InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!