将对象转换为 blob (java.lang.ClassCastException: [B 不能转换为 java.sql.Blob)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15802070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert object to blob (java.lang.ClassCastException: [B cannot be cast to java.sql.Blob)
提问by kitokid
when I try to convert Oject into blob using (Blob), getting that java.lang.ClassCastException: [B cannot be cast to java.sql.Blob error.
当我尝试使用 (Blob) 将 Oject 转换为 blob 时,得到 java.lang.ClassCastException: [B cannot be cast to java.sql.Blob 错误。
but when I try to convert using the following code and write as an image file, gets the image corrupted.
但是当我尝试使用以下代码进行转换并写入图像文件时,图像已损坏。
Blob blob = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(myObj);
byte[] yourBytes = bos.toByteArray();
//blob.setBytes(1, yourBytes );
blob = new SerialBlob(yourBytes);
} finally {
out.close();
bos.close();
}
How can I safely convert object (which is an image file saved as Blob) to Blob?
如何安全地将对象(保存为 Blob 的图像文件)转换为 Blob?
NOTE: due to requirement, I can only get as Object passing from other part.
注意:由于要求,我只能作为从其他部分传递的对象获取。
回答by VKPRO
You can use
您可以使用
If you are using hibernate
如果您使用休眠
Blob blob = Hibernate.createBlob(bytes, session);
If you are using JDBC
如果您使用的是 JDBC
Blob blob = connection.createBlob();
blob.setBytes(1, bytes);
回答by Tirath
Not sure how relevant my answer is but have used the above answers as hint to arrive at this.
不确定我的答案有多相关,但已使用上述答案作为提示来达成此目标。
Using - below dependency
使用 - 下面的依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.0.Final</version>
</dependency>
Property in model class was defined as -
模型类中的属性定义为 -
java.sql.Blob message;
In mymodel.hbm.xml
file inside a valid model mapping
在mymodel.hbm.xml
有效模型映射内的文件中
<class name="packagepath.Pojo" table="pojotableindb">
<meta attribute="class-description">
Pojo class is mapped to pojotableindb table
</meta>
<id name="id" column="ID" type="long">
<!-- for auto increment -->
<generator class="identity" />
</id>
<property name="message" column="MESSAGE" type="blob" />
</class>
Now, inside one of my DAO method I did something as below to insert a value in MESSAGE
column -
现在,在我的 DAO 方法之一中,我做了如下操作在MESSAGE
列中插入一个值-
Pojo pojo = new Pojo();
pojo.setMessageId(uuid);
//message is the string argument this DAO method receives
activityTrack.setMessage(Hibernate.getLobCreator(session).createBlob(message.getBytes()));
tx = session.beginTransaction();
session.save(pojo);
tx.commit();
Hoping this will help someone.
希望这会帮助某人。
回答by Thihara
Because of the class cast exception you are receiving I'm going to go out on a limb here and say you are not actually getting a Blob from that end.
由于您收到的 class cast 异常,我将在这里冒昧地说您实际上并没有从那一端获得 Blob。
Do a debug and see what type is actually being sent.
进行调试并查看实际发送的是什么类型。
And I may be wrong but as far as I know Clobs and Blobs are not serializable... So if the other side is a network, it will be kaboom...
而且我可能是错的,但据我所知,Clobs 和 Blob 是不可序列化的......所以如果另一端是网络,那将是 kaboom......