1

I have a problem reading a blob from a MySQL database with Java. I need to write a webservice with jax-rs to deliver an image saved in the database. For transport, it has to be encoded using Base64.

This is my code:

public String getImage(@PathParam("id") int id) throws SQLException{
    System.out.println(id);
    String img64str = "null";
    Blob image = null;
    Connection conn = MySQLConnection.getInstance();
    if(conn != null)
    {
        try{
        // Anfrage-Statement erzeugen.
        Statement query;
        query = conn.createStatement();

        // Ergebnistabelle erzeugen und abholen.

            String sql = "SELECT bild FROM beitraege where id="+id;
            ResultSet result = query.executeQuery(sql);
            //Ergebniss zur�ckliefern
            while (result.next()) {
                System.out.println("while");
                image = result.getBlob("bild");
                InputStream binaryStream = image.getBinaryStream(1, image.length());
                String str= binaryStream.toString();
                byte[] bdata=str.getBytes();
                byte[] img64 = Base64.encode(bdata);
                img64str = new String(img64);
            }

        }catch (SQLException e) {
            e.printStackTrace();
        }
   }

    return img64str;
}

Somehow, it only returns something like this (shown result list decoded):

java.io.ByteArrayInputStream@cc90a0a
AMIC MING
  • 6,306
  • 6
  • 46
  • 62
FredM
  • 41
  • 1
  • 2
  • 10

4 Answers4

3

java.io.ByteArrayInputStream@cc90a0a is the result of calling toString() on the InputStream. It doesn't actually convert it into a String - it just uses the default toString() behavior of returning the identifier of the object within the current environment.

There are several read() methods defined on the InputStream interface to get at the underlying sequence of bytes represented by the stream. You'll need to use one of those to extract the content, rather than trying to convert it into a String via toString().

Shaun
  • 2,446
  • 19
  • 33
3

The problem lies in the "toString()" call: binaryStream.toString(); BinaryInputStream do not implement toString(). Use something like this to read the bytes:

int ch;

//read bytes from ByteArrayInputStream using read method
while((ch = binaryStream.read()) != -1)
{
   System.out.print((char)ch);
   // store it to an array...
}
Peanut
  • 3,753
  • 3
  • 31
  • 45
0

You are returning the result of binaryStream.toString(), which is something like "java.io.ByteArrayInputStream@cc90a0a".

You have to use the read methods on the InputStream to get the actual content.

jarnbjo
  • 33,923
  • 7
  • 70
  • 94
-2

This line is wrong:

binaryStream.toString();

InputStream does not override toString(), thus uses Object.toString(), which is defined like this. This explains why you're getting that String.

This other question shows you how to correctly convert from InputStream to String.

This said you should not convert binary data (like Base64-encoded image) to string (see NullUserException's comments below).

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • -1 For recommending the conversion of an `InputStream` to a `String`. This is a terrible idea if the `InputStream` doesn't actually contain textual data. – NullUserException Jan 30 '13 at 17:33
  • @NullUserException Nonsense. I'm not recommending it, that's what the OP wants/is asking for. `String getImage()`. – m0skit0 Jan 30 '13 at 17:34
  • 3
    Well, it's wrong. This will mangle the binary data. You should be educating the OP to do it the right way, not mislead them into the wrong path. – NullUserException Jan 30 '13 at 17:36