3 Answers

  1. Martin- Reply

    2019-11-14

    Just get it straight as InputStream and use PreparedStatement#setBinaryStream() to store it. It's binary data, not character data, so a Reader would only messup things, you don't want to have that.

    In a nutshell:

    InputStream input = imageUrl.openStream();
    
    // ...
    
    statement = connection.prepareStatement("INSERT INTO image (content) VALUES (?)");
    statement.setBinaryStream(1, input);
    statement.executeUpdate();
    

    A PreparedStatement is really useful. It not only saves you from SQL injections, but it also eases setting fullworthy Java objects like InputStream in a SQL statement. You can learn more about PreparedStatement at the JDBC tutorial.

  2. Matthew- Reply

    2019-11-14

    Well what I do to store binary data such as images, is quite simplictic. In my case I deal with uploaded files which get posted to a servlet. Inside the servlet, I get the InputStream on the posted body with request.getInputStream(). However, this works with any kind of InputStreawm, inlcuding one based on an URL. The sample code below shows how to convert that InputStream into google appeninge Blob, which then you can for instance make persistant in the data store.

    ...
    import org.apache.commons.io.IOUtils;
    import com.google.appengine.api.datastore.Blob;
    ...
    
    public void InputStreamToBlob(InputStream i) throws IOException {
    
        ...
    
        Blob content = new Blob(IOUtils.toByteArray(i));
        ...
    
  3. Michael- Reply

    2019-11-14

    For an image, instead of reading the buffer line by line, you'll load it in byte array, and save this byte array as a blob.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>