19

For an applet I'm working on I need to convert a BufferedImage file to an input stream so that I can upload the image to my MySQL server. Originally I was using this code:

Class.forName("com.mysql.jdbc.Driver").newInstance();  
Connection connection = 
    DriverManager.getConnection(connectionURL, "user", "pass");  

psmnt = connection.prepareStatement(
    "insert into save_image(user, image) values(?,?)");  
psmnt.setString(1, username);  

ImageIO.write(image, "png", new File("C://image.png")); 
File imageFile = new File("C://image.png");
FileInputStream fis = new FileInputStream(imageFile);

psmnt.setBinaryStream(2, (InputStream)fis, (fis.length()));
int s = psmnt.executeUpdate();

if(s > 0) {
  System.out.println("done");
}

(while catching the relevant exceptions) The code hangs on the part where the applet attempts to save the image to the computer. The code worked perfectly in Eclipse or whenever I ran the applet from the localhost, so I'm assuming the problem is in the privileges that the applet has in saving files to the user's computer.

I was just was wondering if there was a way to turn the image file into an inputstream without having to save a file to the user's computer. I tried using:

ImageIO.createImageInputStream(image);

But then I couldn't convert the ImageInputStream back to an InputStream. Any Suggestions?

Thanks!

2
  • Yeah it's probably not the best practice. Thanks for the info, I'll have to fix that later. Commented Jan 31, 2011 at 17:48
  • Possible duplicate of How to convert BufferedImage to InputStream? Commented Nov 11, 2015 at 10:04

3 Answers 3

33

Typically you would use a ByteArrayOutputStream for that purpose. It acts as an in-memory stream.

ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image,"png", os); 
InputStream fis = new ByteArrayInputStream(os.toByteArray());
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome but one more thing, I need the length of the stream in order to create the BinaryStream, so would I use os.toByteArray().length?
it worked perfectly. I used os.toByteArray().length and the code above.
I have a webapp where users upload images. I want to do some validation and manipulation of the images before storing in an S3 bucket. Is it best to use the code above? Or simply write to a File object?
2

Have you tried writing to a ByteArrayOutputStream and then creating a ByteArrayInputStream from that data to read from? (Call toArray on the ByteArrayOutputStream and then call the constructor of ByteArrayInputStream which will wrap that byte array.)

1 Comment

Thanks for the suggestion, that's basically what I ended up doing but the other answer actually baby-fed me the code. :P
1

Be careful using BytArray streams: if the image is large, that code will fail. i have not done much applet coding, but it's possible that the temp dir is available for writing (e.g. File.createTempFile() ).

1 Comment

So far it's worked, if it doesn't though, then I'll have to use that. Thanks

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.