转换大尺寸jpg图像时抛出异常

时间:2011-09-02 15:30:54

标签: java

我正在尝试上传jpg 图片&通过servlet将其转换为新的缩略图。 在磁盘上写入该文件后,我的要求是为同一个jpg文件创建一个缩略图。 为此,我使用了以下代码......

//code to upload & write image file on disk goes here..

BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
img.createGraphics().drawImage(ImageIO.read(new File(sourceFile)).getScaledInstance(100, 100, Image.SCALE_SMOOTH),0,0,null);

ImageIO.write(img, "jpg", new File(thumbFile));

以上代码适合小文件。但是当它出现大小 5MB 的文件时,它会复制,但无法将其转换为缩略图&抛出

Exception in thread ""http-bio-8888"-exec-63" java.lang.OutOfMemoryError: Java heap space
    at java.awt.image.DataBufferByte.<init>(Unknown Source)
    at java.awt.image.ComponentSampleModel.createDataBuffer(Unknown Source)
    at java.awt.image.Raster.createWritableRaster(Unknown Source)
    at javax.imageio.ImageTypeSpecifier.createBufferedImage(Unknown Source)
    at javax.imageio.ImageReader.getDestination(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
    at UploadFile.doPost(UploadFile.java:67).

请指导我。

1 个答案:

答案 0 :(得分:1)

您可以使用java解释器的'-Xms'和-Xmx命令行选项来增加堆大小(分别设置初始堆大小和最大堆大小)。

Playing with JVM / Java Heap Size上查看此博客条目。