I am trying to upload the jpg images & converting same int开发者_开发问答o new thumbnail through servlet. After writing that file on disk, my requirment is to create a thumbnail for the same jpg file. And for that I used following code...
//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));
Above code goes well for small files. But when its comes towards file of size 5MB, it copies, but fails to convert it into thumbnail & throws
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).
Please guide me.
You can increase the heap size by using the '-Xms' and -Xmx
command line option to the java interpreter (sets initial heap size and maximum heap size, respectively).
Check out this blog entry on Playing with JVM / Java Heap Size.
精彩评论