如何将未知格式的 Blob 对象转换为字符串或 java 中的 xml

How to convert Blob object of unknown format to String or xml in java

我有一个 blob 对象,我正试图将其转换为字符串或 XML 格式。我无法正确序列化它,因为我不知道格式。如何转换此 blob?

 Blob blob = rs.getBlob(4);
 StringBuffer strOut = new StringBuffer();
 String aux = null;
 BufferedReader br = new BufferedReader(new Inputblob.getBinaryStream()));
try {
    while ((aux = br.readLine()) != null) {
        strOut.append(aux);
    }
    } 
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

System.out.println(aux);

输出以某种奇怪的字符格式出现:

由于您不知道结构,将对象反序列化为字符串没有太大意义。

所以如果你的数据集很小。你可以用这个。

java.sql.Blob ablob = rs.getBlob(1);
String str = new String(ablob.getBytes(1l, (int) ablob.length()));

如果数据大小超过,则有可能出现 OutOfMemoryExceptions。

你可以试试上面的代码,如果不行你可以试试这个。

StringBuffer strOut = new StringBuffer();
String aux;
// We access to stream, as this way we don't have to use the CLOB.length() which is slower...
// assuming blob = resultSet.getBlob("somefield");
BufferedReader br = new BufferedReader(new InputStreamReader(blob.getBinaryStream()));
while ((aux=br.readLine())!=null) {
strOut.append(aux);
}
return strOut.toString();

试试这个,如果这些有效,请告诉我。

Blob 为可移植对象格式,这意味着数据已被序列化,因此我必须将其反序列化为 java 对象。

                        Blob blob = rs.getBlob(4);

                            int blobLength = (int) blob.length();  
                            byte[] blobAsBytes = blob.getBytes(1, blobLength);
                            //release the blob and free up memory. (since JDBC 4.0)
                            blob.free();

                            ByteBuffer buff = ByteBuffer.wrap(blobAsBytes);

                            Object obj = PofSerializerHelper.deserialize1(buff); 

这是所希望的,所以问题已解决。谢谢