MySqlにおけるBlobバイナリオブジェクトの処理
3517 ワード
BLOB(binary large object)は、バイナリオブジェクトで、バイナリファイルを格納できるコンテナです.
画像などの情報を格納するために使用できます
Demo 1:画像を保存
Demo 2:画像取得
転載先:https://www.cnblogs.com/liuwt365/p/4098313.html
画像などの情報を格納するために使用できます
Demo 1:画像を保存
1 String sql="INSERT INTO TestBlob(NAME,headImagfe) VALUES (?,?)";
conn=JdbcUtil.getConnection();
2 pstmt=conn.prepareStatement(sql);
3 pstmt.setString(1,"mm");
4
5 //mysql , ,
6 //pstmt.setBlob(parameterIndex, inputStream, length)
7
8 InputStream is=new FileInputStream("D:\\a.jpg");//
9
10 pstmt.setBinaryStream(2, is, is.available());// sql
11
12 pstmt.executeUpdate();//
Demo 2:画像取得
1 String sql="select * from TestBlob where id=1";
2 Blob blob= rs.getBlob("headImagfe");
3 InputStream is=blob.getBinaryStream();//
4 String path="D:\\b.jpg";
5 OutputStream out=new FileOutputStream(path);//
6 int len=-1;
7 byte[] buffer=new byte[1024];
8 while ((len=(is.read(buffer)))>0) {
//
9 out.write(buffer,0,len);
10 }
11 out.close();
12 is.close();
転載先:https://www.cnblogs.com/liuwt365/p/4098313.html