首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

数据间变换

2012-12-21 
数据间转换//byte[] 转换为 Blob?Blob blob null?byte[] buf null?blobnew SerialBlob(buf)??// i

数据间转换

//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;
??? }

热点排行