[英]How to convert Blob object of unknown format to String or xml in java
I have a blob object which I am trying to convert to String or XML format.我有一个 blob 对象,我试图将其转换为 String 或 XML 格式。 I am not able to properly serialize it as I don't know the format.我无法正确序列化它,因为我不知道格式。 How can I convert this blob?我怎样才能转换这个 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);
The output comes in some strange character format:输出采用一些奇怪的字符格式:
The Blob was in Portable Object Format it means that the data has been serialized so I must deserialise it to a java object. 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);
This is was as desired, so issue solved.这是如愿以偿,所以问题解决了。 Thanks谢谢
As you do not know the structure, the de-serializing of the object into string would not make too much sense.由于您不知道结构,因此将对象反序列化为字符串并没有太大意义。
So if you data set is small.所以如果你的数据集很小。 You can use this.你可以用这个。
java.sql.Blob ablob = rs.getBlob(1);
String str = new String(ablob.getBytes(1l, (int) ablob.length()));
if the data size exceeds then there are chances to get OutOfMemoryExceptions.如果数据大小超过那么有机会得到 OutOfMemoryExceptions。
You can try the code above, if that fails you could try this.你可以试试上面的代码,如果失败你可以试试这个。
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();
Do try this and let me know if these work.试试这个,让我知道这些是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.