数据间转换
//byte[] 转换为 Blob
?Blob blob = null;
?byte[] buf = null;
?blob=new SerialBlob(buf);
?
?
// inputStream 转换为 byte[]
?public byte[] InputStreamToByte(InputStream iStrm) throws IOException
?{
???? ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
???? int ch;
???? while ((ch = iStrm.read()) != -1)
???? {
??????? bytestream.write(ch);
???? }
???? byte imgdata[]=bytestream.toByteArray();
???? bytestream.close();
???? return imgdata;
?}
?
?
// byte[] 转换为 inputStream
byte[]?data; ??
InputStream?is?=?new?ByteArrayInputStream(data);??
?
?
?
?public static void writeBinaryStreamToBlob(byte byte[], Blob blob)
??????? throws SQLException, IOException
??? {
??????? if(byte!= null)
??????? {
??????????? OutputStream outputStream = blob.setBinaryStream(0L);
??????????? outputStream.write(byte);
??????????? outputStream.flush();
??????????? outputStream.close();
??????? }
??? }
?
??? public static byte[] getBinaryStreamFromBlob(Blob blob)
??????? throws SQLException, IOException
??? {
??????? byte blobByte[] = null;
??????? if(blob != null)
??????? {
??????????? InputStream inputStream = blob.getBinaryStream();
??????????? ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
??????????? for(int j = -1; (j = inputStream.read()) != -1;)
??????????????? byteArrayOutputStream.write(j);
??????????? blobByte = byteArrayOutputStream.toByteArray();
??????? }
??????? return blobByte;
??? }