我将图像的byte []转换为具有已定义宽度和高度的jpg,并将该图像存储在我的amazon s3上,但是我刚刚发现java上的某些内容正在“挂起”#39;就在我使用ImageIO.read指令的行之后。我只注意到它,因为在调用ImageIO.read行时java控制台会打开,但在关闭我的应用程序之前永远不会关闭。我已经检查过关闭我在代码中找到的所有缓冲区和流,但即使图像保存在亚马逊上,控制台仍会打开。有没有办法检查发生了什么?我一直在阅读stackoverflow,ImageIO.read有一些内存泄漏问题。
这是我的代码:
private byte[] transformImageToJpg(byte[] receivedImage, int width, int height) {
if (receivedImage == null)
return null;
InputStream is = null;
BufferedImage bufferedImage = null;
byte[] imageInByte = null;
try {
is = new ByteArrayInputStream(receivedImage);
bufferedImage = ImageIO.read(is);
if(bufferedImage == null) {
receivedImage = null;
is.close();
showError("Invalid File", "The selected file is not a valid image file.");
return null;
}
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
BufferedImage newBufferedImage = new BufferedImage(width,
height, BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, width, height, 0, 0, w, h, Color.WHITE, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(newBufferedImage, "jpg", baos);
baos.flush();
imageInByte = baos.toByteArray();
newBufferedImage.flush();
bufferedImage.flush();
baos.close();
is.close();
receivedImage = null;
} catch (IOException e) {
e.printStackTrace();
}
return imageInByte;
}