posts - 3, comments - 15, trackbacks - 0, articles - 26
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

数据库存取文件

Posted on 2005-06-10 22:20 morcble的blog 阅读(276) 评论(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();
   
   

只有注册用户登录后才能发表评论。


网站导航: