最近因为自己个人的一个需求,就是把文件存入数据库中,以方便备份,所以就写了这个类,这个类也不完全原创,有参考网上的一些资料,但通过自己的测试,完全可以实现自己的需求,所以把代码贴出来分享一下!

    以下的一些路径、数据库表名、字段名 需要自己修改一下,也可以根据自己的需要改成配置的!这个看自己的啦!还有就是那个jdbc工具类自己改一下!

1:WriteAndReadFile 

 
  
  1. package com.grg.johnny.work; 
  2.  
  3. import java.io.*; 
  4. import java.sql.*; 
  5.  
  6. import oracle.sql.BLOB; 
  7.  
  8. public class WriteAndReadFile { 
  9.      
  10.     public static void main(String[] args){ 
  11.         //1:先写入 
  12.         String path = "D://oracle2.zip"
  13.         saveFile(path);  
  14.         //2:再读出 
  15.         //getFile("1"); 
  16.          
  17.     } 
  18.  
  19.     /** 
  20.      * 写入文件 
  21.      * 往blob里插入文件要先插入空值empty_blob(),再进行修改 
  22.      * @param filePath 
  23.      * @return 
  24.      */ 
  25.     public static boolean saveFile(String filePath) { 
  26.         File file = new File(filePath); 
  27.         Connection conn = JdbcUtil.getConnection(); 
  28.         try { 
  29.             java.sql.Statement st = conn.createStatement(); 
  30.             conn.setAutoCommit(false); 
  31.             System.out.println("=====================save file begin========================"); 
  32. //          st.execute("insert into johnny_file values(1,'日常生活',empty_blob(),'这是一个很强大的文件',to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'))"); 
  33.             st.execute("insert into johnny_file values(2,'日常生活2',empty_blob(),'这是一个很强大的文件test',to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'))"); 
  34.             ResultSet rs = st 
  35.                     .executeQuery("select id,file_blob from johnny_file where id=2 for update"); 
  36.             if (rs.next()) { 
  37.                 BLOB blob = (BLOB) rs.getBlob("file_blob"); 
  38.                 OutputStream outStream = blob.getBinaryOutputStream(); 
  39.                 InputStream fin = new FileInputStream(file); 
  40.                 byte[] b = new byte[blob.getBufferSize()]; 
  41.                 int len = 0
  42.                 while ((len = fin.read(b)) != -1) { 
  43.                     outStream.write(b, 0, len); 
  44.                 } 
  45.                 fin.close(); 
  46.                 outStream.flush(); 
  47.                 outStream.close(); 
  48.                 conn.commit(); 
  49.                 conn.close(); 
  50.             } 
  51.         } catch (SQLException e) { 
  52.             // TODO Auto-generated catch block 
  53.             e.printStackTrace(); 
  54.             return false; 
  55.         } catch (FileNotFoundException e) { 
  56.             // TODO Auto-generated catch block 
  57.             e.printStackTrace(); 
  58.             return false; 
  59.         } catch (IOException e) { 
  60.             // TODO Auto-generated catch block 
  61.             e.printStackTrace(); 
  62.             return false; 
  63.         } 
  64.         System.out.println("=====================save file end========================"); 
  65.         return true; 
  66.     } 
  67.  
  68.     /** 
  69.      * 读取 
  70.      * 读取出的路径根据自己的需要修改 
  71.      * @param id 
  72.      */ 
  73.     public static void getFile(String id) { 
  74.         Connection conn = JdbcUtil.getConnection(); 
  75.         java.sql.Statement st; 
  76.         try { 
  77.             st = conn.createStatement(); 
  78.             System.out.println("=====================get file begin========================"); 
  79.             ResultSet rs = st 
  80.                     .executeQuery("select id,file_blob from johnny_file where id='" 
  81.                             + id + "'"); 
  82.             if (rs.next()) { 
  83.                 BLOB blob = (BLOB) rs.getBlob("file_blob"); 
  84.                 File file = new File("D://oracle.zip"); 
  85.                 FileOutputStream output = new FileOutputStream(file); 
  86.                 InputStream input = blob.getBinaryStream(); 
  87.                 byte[] buffer = new byte[1024]; 
  88.                 int i = 0
  89.                 while ((i = input.read(buffer)) != -1) { 
  90.                     output.write(buffer, 0, i); 
  91.                 } 
  92.             } 
  93.             System.out.println("=====================get file end========================"); 
  94.         } catch (Exception e) { 
  95.             // TODO Auto-generated catch block 
  96.             e.printStackTrace(); 
  97.         } 
  98.     } 
  99.  
  100.     /** 
  101.      * 修改blob内容 
  102.      */ 
  103.     public static void updateblob(String id) { 
  104.         Connection conn = JdbcUtil.getConnection(); 
  105.         Statement stem = null
  106.         ResultSet rs = null
  107.         try { 
  108.             conn.setAutoCommit(false); 
  109.             stem = conn.createStatement(); 
  110.             System.out.println("=====================update file begin========================"); 
  111.             rs = stem.executeQuery("select file_blob from johnny_file where id='" 
  112.                     + id + "' for update"); 
  113.  
  114.             if (rs.next()) { 
  115.                 BLOB blob = (BLOB) rs.getBlob("file_blob"); 
  116.                 OutputStream outStream = blob.getBinaryOutputStream(); 
  117.                 InputStream fin = new FileInputStream("D://ok.zip"); 
  118.                 byte[] b = new byte[blob.getBufferSize()]; 
  119.                 int len = 0
  120.                 while ((len = fin.read(b)) != -1) { 
  121.                     outStream.write(b, 0, len); 
  122.                 } 
  123.                 fin.close(); 
  124.                 outStream.flush(); 
  125.                 outStream.close(); 
  126.                 conn.commit(); 
  127.                 conn.close(); 
  128.             } 
  129.             System.out.println("=====================update file end========================"); 
  130.         } catch (Exception ex) { 
  131.             try { 
  132.                 conn.rollback(); 
  133.             } catch (SQLException e) { 
  134.                 // TODO Auto-generated catch block 
  135.                 e.printStackTrace(); 
  136.             } 
  137.             System.out.println(ex.getMessage()); 
  138.         } 
  139.     } 

 

2:以下这个是我自己的jdbc工具类

 

 
  
  1. package com.grg.johnny.work; 
  2.  
  3. import java.sql.*; 
  4.  
  5. public class JdbcUtil { 
  6.     /** 
  7.      * driverName 
  8.      * */ 
  9.     static{ 
  10.         String driverName="oracle.jdbc.driver.OracleDriver"
  11.         try{ 
  12.             Class.forName(driverName); 
  13.         }catch(Exception e){ 
  14.             e.printStackTrace(); 
  15.         } 
  16.     } 
  17.      
  18.     /** 
  19.      * getConnection 
  20.      * */ 
  21.     public static Connection getConnection(){ 
  22.         String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"
  23.         String userName="XXXXX"
  24.         String pwd="XXXXXX"
  25.         Connection con = null
  26.         try{ 
  27.             con = DriverManager.getConnection(url,userName,pwd); 
  28.         }catch(Exception ee){ 
  29.             ee.printStackTrace(); 
  30.         } 
  31.         return con; 
  32.     } 
  33.      
  34.     /** 
  35.      * close 
  36.      * */ 
  37.     public static void close(ResultSet rs,Statement stmt,Connection con){ 
  38.         try{ 
  39.             if(rs!=null){rs.close();} 
  40.         }catch(Exception ee){ 
  41.             ee.printStackTrace(); 
  42.         } 
  43.         try{ 
  44.             if(stmt!=null){stmt.close();} 
  45.         }catch(Exception ee){ 
  46.             ee.printStackTrace(); 
  47.         } 
  48.         try{ 
  49.             if(con!=null){con.close();} 
  50.         }catch(Exception ee){ 
  51.             ee.printStackTrace(); 
  52.         } 
  53.     } 
  54.