Reading a blob from MySQL with Java

这一生的挚爱 提交于 2019-12-23 02:38:54

问题


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

回答1:


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



回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/14610011/reading-a-blob-from-mysql-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!