用java实现 将oracle数据库表中的文件取出来到本地

文件的格式很多.jpg . doc 等等等 在线等,问题完成发微信红包30元谢谢啦 急等

给你一段读图片的代码,其他的DOC什么的其实就是文件的后缀名改下而已。BLOB字段其实就是用2进制流保存了文件,你用java的IO流去读取然后写到FILE里,给个对应的后缀就好了。


//存入数据库
Connection conn = null;
  ResultSet rs = null;
  Statement stmt = null;   
  DBConnectionManager connMgr = null;
  try 
  {
   String insertsql = "insert into image (id,pic) values(18,empty_blob())";//首先插入一个空的Blob类型
   String updateSql="select fujiannr from T_ZT_MYZJ_MYCJ where id=18 for update";            
   connMgr = DBConnectionManager.getInstance();
   conn = connMgr.getConnection(); 
   conn.setAutoCommit(false);
   OutputStream os = null;   
   stmt=conn.createStatement();
   stmt.executeUpdate(insertsql);
   rs=stmt.executeQuery(updateSql);
   if (rs.next()) 
   {  
    oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("fujiannr");   
     os = blob.getBinaryOutputStream();   
     InputStream is1= new FileInputStream("c:\\1.png");   
     int i = 0;  
     while ((i = is1.read()) != -1) {   
      os.write(i);   
     }
   }
    os.flush();
       os.close();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  finally {
   closeAll(rs, stmt,conn);
  }
  
//失放资源
  ClassLoader cl = this.getClass().getClassLoader();
  InputStream pro = cl.getResourceAsStream("\\config.properties");
  Properties dbProps = new Properties();
  try {
   dbProps.load(pro);
  } catch (Exception e) {
  }
  String driverClasses = dbProps.getProperty("dbDriver");
  connMgr.freeConnection(driverClasses, conn);
 
//从数据库中读取图片
  Connection conn = null;
  ResultSet rs = null;
  Statement stmt = null;   
  InputStream issgg = null;
  DBConnectionManager connMgr = null;
  byte bs[] = null;
  
  try 
  {           
   connMgr = DBConnectionManager.getInstance();
   conn = connMgr.getConnection(); 
   conn.setAutoCommit(false);
     
   stmt=conn.createStatement();
   String str = "select pic from image where id=18";  
   rs=stmt.executeQuery(str);
   rs.next();
   Blob ob = null;
   ob=rs.getBlob(1);
   long size = ob.length();
   bs = ob.getBytes(1, (int)size);  
 
   File file = new File("c:\\" + "image.png");
         if (!file.exists())
         { 
             file.createNewFile(); // 如果文件不存在,则创建 
         }
         FileOutputStream fos = new FileOutputStream(file); 
 
         if (data.length > 0) 
         { 
             fos.write(bs, 0, bs.length); 
         }
         else
         { 
             //while ((size = in.read(bs)) != -1) 
            // { 
               ///  fos.write(bs, 0, size); 
            // } 
            // in.close(); 
         } 
         fos.close();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  finally {
   closeAll(rs, stmt,conn);
  }

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答