2

I am creating the image from inputStream and I have used two methods but both the methods fails.

1st Method:

FileOutputStream out=new FileOutputStream(file)
byte[] byteBuffer = new byte[1024];
int length = 0;
while ((stream != null) && ((length = stream.read(byteBuffer)) != -1)){
out.write(byteBuffer,0,length);
}
out.flush()
out.close();
stream.close();

This method generates the image but while trying to open it. It says "This is not a valid bitmap file".

And the 2nd method:

BufferedImage imBuff = ImageIO.read(stream);
ImageIO.write(imBuff, 'png', file);

And this one generates the exception.

java.lang.IllegalArgumentException: image == null! at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpe cifier.java:925) at javax.imageio.ImageIO.getWriter(ImageIO.java:1591) at javax.imageio.ImageIO.write(ImageIO.java:1520) at com.k12report.frontend.ClientAuthoringController$$EOU2Lt2o.getImages( ClientAuthoringController.groovy:55) at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(Pag eFragmentCachingFilter.java:195) at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter .java:63) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor. java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor .java:615) at java.lang.Thread.run(Thread.java:722)

Thanks

4
  • 2
    What is stream? What is it's contents? Have you tried dumping the contents to disk and verifying it's validity through an external image program? Commented Jan 26, 2014 at 8:25
  • stream = response.getEntityInputStream() Commented Jan 26, 2014 at 8:26
  • 2
    Are you sure you have a valid image to begin with? In the 2nd method, imBuff is null, this happens when ImageIO don't recognize the file format. You need to test for that situation. Your 1st method looks good to me. You should probably check for stream != null outside the loop for readability and performance though. Commented Jan 26, 2014 at 8:59
  • who said: This is not a valid bitmap file? (this is not clear to me...) Commented May 8, 2015 at 14:54

1 Answer 1

1

Well, for the this is not a valid bitmap file

Using File.separator is the more recommended way instead of using "\" or "/" if you are using file paths as they depend on OS.They make your code more portable.

Example:-

File f = new File("C:"+File.separator+"Personal"+File.separator+"test.bmp");

Sign up to request clarification or add additional context in comments.

2 Comments

I am using grails so, def root = getServletContext().getRealPath(""); String path = root+'/authoringImages/'+images File file = new File(path);
for me, using / as file separator works always (when working with File-Object)...also when using windows....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.