数据库中提供了两种字段类型 Blob 和 Clob 用于存储大型字符串或二进制数据(如图片)。
Blob 采用单字节存储,适合保存二进制数据,如图片文件。
Clob 采用多字节存储,适合保存大型文本数据。
首先创建一个空 Blob/Clob 字段,再从这个空 Blob/Clob字段获取游标,例如下面的代码:
PreparedStatement ps = conn.prepareStatement( " insert into PICTURE(image,resume) values(?,?) " )
// 通过oralce.sql.BLOB/CLOB.empty_lob()构造空Blob/Clob对象
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob())
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob())
ps.excuteUpdate()
ps.close()
// 再次对读出Blob/Clob句柄
ps = conn.prepareStatement( " select image,resume from PICTURE where id=? for update " )
ps.setInt( 1 , 100 )
ResultSet rs = ps.executeQuery()
rs.next()
oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 )
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 )
// 将二进制数据写入Blob
FileInputStream inStream = new FileInputStream( " c://image.jpg " )
OutputStream outStream = imgBlob.getBinaryOutputStream()
byte [] buf = new byte [ 10240 ]
int len
while (len = inStream.read(buf) >0 ) {
outStream.write(buf, 0 ,len)
}
inStream.close()
outStream.cloese()
// 将字符串写入Clob
resClob.putString( 1 , " this is a clob " )
// 再将Blob/Clob字段更新到数据库
ps = conn.prepareStatement( " update PICTURE set image=? and resume=? where id=? " )
ps.setBlob( 1 ,imgBlob)
ps.setClob( 2 ,resClob)
ps.setInt( 3 , 100 )
ps.executeUpdate()
ps.close()
这个简单啊(1)把获取url流转为bitmap
(2)把bitmap再转为base64
public static Bitmap getBitMBitmap(String urlpath) {
Bitmap map = null
try {
URL url = new URL(urlpath)
URLConnection conn = url.openConnection()
conn.connect()
InputStream in
in = conn.getInputStream()
map = BitmapFactory.decodeStream(in)
// TODO Auto-generated catch block
} catch (IOException e) {
e.printStackTrace()
}
return map
}
第二步
/**
* bitmap转为base64
* @param bitmap
* @return
*/
public static String bitmapToBase64(Bitmap bitmap) {
String result = null
ByteArrayOutputStream baos = null
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
baos.flush()
baos.close()
byte[] bitmapBytes = baos.toByteArray()
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT)
}
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
if (baos != null) {
baos.flush()
baos.close()
}
} catch (IOException e) {
e.printStackTrace()
}
}
return result
}
有什么问题提问就好
java是可以将图片字节转成图片的,但是你需要知道接受的数据对应一个图片有多长,可以在每个图片数据包前面加上此次图片的长度,按长度截取字节,转成图片,或者直接将字节转成视频,毕竟你是连续的图像