Clojure 从数据库读取 Blob
我需要从这个 Blob 读取字节。我正在尝试以下操作,但出现此异常: oracle.sql.BLOB 无法转换为 [B
(defn select-test2[]
(clojure.contrib.sql/with-connection db
(with-query-results res ["SELECT my_blob from some_table"] (doall res))))
(defn obj [byte-buffer]
(if-not (nil? byte-buffer)
(with-open [object-in (ObjectInputStream.
(ByteArrayInputStream. byte-buffer))]
(.readObject object-in))))
(obj (:my_blob (first (select-test2))))
I need to read bytes from this Blob. I'm trying the following but I'm getting this exception:
oracle.sql.BLOB cannot be cast to [B
(defn select-test2[]
(clojure.contrib.sql/with-connection db
(with-query-results res ["SELECT my_blob from some_table"] (doall res))))
(defn obj [byte-buffer]
(if-not (nil? byte-buffer)
(with-open [object-in (ObjectInputStream.
(ByteArrayInputStream. byte-buffer))]
(.readObject object-in))))
(obj (:my_blob (first (select-test2))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[B
是字节数组的“类”:因此,代码中的某个位置需要字节数组,但它被赋予一个
oracle.sql.Blob
实例。我敢打赌:my_blob
会给你一个Blob
;当您将byte-buffer
(即Blob
)传递给ByteArrayInputStream
构造函数时,您会遇到异常。查找
oracle.sql.Blob
的 javadocs 以了解如何从中提取字节数组或输入流。[B
is the "class" of a byte array:So there's a place in your code that is expecting a byte array, but it's being given an
oracle.sql.Blob
instance. My bet is that:my_blob
is giving you aBlob
; when you passbyte-buffer
(which is theBlob
) to theByteArrayInputStream
constructor, you get the exception.Look up the javadocs for
oracle.sql.Blob
to see how to extract a byte array or input stream from it.