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!

David
  • 432
  • 1
  • 5
  • 11
  • Yeah it's probably not the best practice. Thanks for the info, I'll have to fix that later. – David Jan 31 '11 at 17:48
  • Possible duplicate of [How to convert BufferedImage to InputStream?](http://stackoverflow.com/questions/4251383/how-to-convert-bufferedimage-to-inputstream) – Francisco Puga Nov 11 '15 at 10:04

3 Answers3

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());
Carles Barrobés
  • 11,608
  • 5
  • 46
  • 60
  • 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? – David Jan 31 '11 at 17:35
  • it worked perfectly. I used os.toByteArray().length and the code above. – David Jan 31 '11 at 17:46
  • 1
    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? – theyuv Nov 19 '16 at 19:30
1

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Thanks for the suggestion, that's basically what I ended up doing but the other answer actually baby-fed me the code. :P – David Jan 31 '11 at 17:48
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() ).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118