将BufferedImage转换为InputStream,亲测可用

时间:2021-10-11 19:38:55
private static final Logger logger = Logger.getLogger(Demo.class);

/**
* 将BufferedImage转换为InputStream
* @param image
* @return
*/
public InputStream bufferedImageToInputStream(BufferedImage image){
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", os);
InputStream input = new ByteArrayInputStream(os.toByteArray());
return input;
} catch (IOException e) {
logger.error("提示:",e);
}
return null;
}