プログラム開発では、java.sql.blob、byte []、およびinputstreamの間の相互変換をよく使用しますが、JDK APIでは、利用可能なAPIを直接提供しません。次のプログラムフラグメントは、主にそれらの間の交換可能なユーティルを実装します。
1。byte[] => blob
次のような、Hibernateが提供するステートメントメソッドを実装できます。
org.hibernate.hibernate.hibernate.createblob(new byte [1024]);
2。blob=> byte []
現在、より良いAPIは見つかりませんので、自分で実装することしかできません。例は次のとおりです。
/ ** *ブロブタイプをバイト配列に変換します * @param blob * @return */ private byte [] blobtobytes(blob blob){bufferedinputStream is = null; try {is = new BufferedInputStream(blob.getBinaryStream()); byte [] bytes = new byte [(int)blob.length()]; int len = bytes.length; int offset = 0; int read = 0; while(offset <len &&(read = is.read(bytes、offset、len -offset))> = 0){offset += read; }バイトを返します。 } catch(例外E){nullを返します。 }最後に{try {is.close(); IS = null; } catch(ioException e){return null; }}}3。inputstream => byte []
private byte [] inputstreamtobyte(inputstream is)throws ioexception {bytearrayoutputStream byTestream = new bytearrayoutputStream(); int ch; while((ch = is.read())!= -1){bytestream.write(ch); } byte imgdata [] = bytestream.tobytearray(); bytestream.close(); imgdataを返します。 }4。byte[] => inputstream
byte []へのinputstreamへの変換は単純です。inuptStreamは= new bytearrayinputStream(new byte [1024]);
5。inuptStream=> blob
hibernate:hibernate.createblob(new fileinputStream( "写真/ファイルなどのパスにすることができる");
6。BLOB=> inputStream
ブログ転送。提供されたAPIを介して直接呼び出すことができます:new Blob()。getBinaryStream();
上記のクリップは、読者のリファレンスとして使用できます。
読んでくれてありがとう、私はそれがあなたを助けることができることを願っています。このサイトへのご支援ありがとうございます!