Posted on 2005-06-10 22:20
morcble的blog 閱讀(291)
評論(0) 編輯 收藏 所屬分類:
Java
數據庫存文件
File file = new File("c:\\2.jpg");
int fileLength =(int) file.length();
InputStream fin = new FileInputStream(file);
PreparedStatement pstmt = con.prepareStatement("insert into file values('2.jpg',?)");
pstmt.setBinaryStream (1, fin, fileLength);
pstmt.executeUpdate();
數據庫取文件
//"select * from file"
Statement pstmt = con.createStatement();
ResultSet rs = pstmt.executeQuery("select * from file");
while(rs.next()){
Blob blob = rs.getBlob("aaa");//aaa為blob列 存儲二進制文件
String name = rs.getString("v");//v為文件名
int bloblength =(int) blob.length();
byte[] bytes = blob.getBytes(1,bloblength);
OutputStream f0 = new FileOutputStream("c:\\1\\"+name);
for (int i =0;i<bytes.length;i+=1){
f0.write(bytes[i]);
}
f0.close();