更新用blob字段存储照片的Java代码

  1. package com.service;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.sql.Blob;  
  10. import java.sql.Connection;  
  11. import java.sql.DriverManager;  
  12. import java.sql.PreparedStatement;  
  13. import java.sql.ResultSet;  
  14. import java.sql.SQLException;  
  15. import java.util.List;  
  16. import java.util.ArrayList;  
  17. import oracle.sql.BLOB;  
  18.   
  19. import com.litsoft.cctv.util.propertiesUtil;  
  20. /** 
  21.  * 由于需要将两个不同库的的照片进行更新,表里面存照片的字段是blob类型的, 
  22.  * 琢磨来一下,直接将结果select查询出来再update行不通,所以先将图片从库里 
  23.  * 导到本地,然后在从本地拿数据进行更新操作  
  24.  * 
  25.  */  
  26. public class UpdateImage{  
  27.     /** 
  28.      * 我把数据库的连接信息写到properties文件里 
  29.      */  
  30.     private String toUrl = "repast.url";  
  31.     private String toUserName= "repast.username";  
  32.     private String toPassword = "repast.password";  
  33.       
  34.     private String fromUrl = "manage.url";  
  35.     private String fromUserName = "manage.username";  
  36.     private String fromPassword = "manage.password";  
  37.   
  38.     /** 
  39.      * @param args 
  40.      * @throws Exception  
  41.      */  
  42.     public static void main(String[] args) throws Exception {  
  43.         CopyOfImportImg i = new CopyOfImportImg();  
  44.         i.exportImage();  
  45.         List<String> accounts = readfile("D:\\image");  
  46.         i.updateImage(accounts);  
  47.     }  
  48.       
  49.     /** 
  50.      * 导出照片到本地 
  51.      */  
  52.     public void exportImage(){  
  53.         long start = System.currentTimeMillis();  
  54.         Connection conn = null;  
  55.         PreparedStatement  preparedStatement = null;  
  56.         ResultSet resultSet = null;  
  57.          //加载驱动  
  58.         try {  
  59.             DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
  60.           
  61.         //获得连接(源),其中propertiesUtil是获取properties文件的一个工具类,在这里略过  
  62.         conn=DriverManager.getConnection(  
  63.                 propertiesUtil.getProperties(fromUrl),propertiesUtil.getProperties(fromUserName),propertiesUtil.getProperties(fromPassword));  
  64.           
  65.        //取出所有的照片记录  
  66.        preparedStatement = conn.prepareStatement("select t.employee_number,t.photofile from oadb.SAP_PHOTO t where t.end_date = '99991231' ");  
  67.        preparedStatement.execute();   
  68.            resultSet = preparedStatement.getResultSet();  
  69.           
  70.            while(resultSet.next()){  
  71.                         //工号  
  72.             String account = resultSet.getString("employee_number");              
  73.                 //照片  
  74.             Blob photo = resultSet.getBlob("photofile");      
  75.             InputStream inputStream = photo.getBinaryStream();    
  76.             //将照片放在D盘下,文件名如:002125457.jpg  
  77.             File fileOutput =  
  78.                            new File("D:\\image\\"+account+".jpg");  
  79.               
  80.             if(!fileOutput.exists()){  
  81.                 fileOutput.createNewFile();  
  82.             }  
  83.               
  84.             FileOutputStream fo =  
  85.                   new FileOutputStream(fileOutput);  
  86.             int c;  
  87.             while ((c = inputStream.read()) != -1) {  
  88.                 fo.write(c);  
  89.             }  
  90.             fo.close();  
  91.         }  
  92.         } catch (Exception e) {           
  93.             e.printStackTrace();  
  94.         }finally{  
  95.             try {  
  96.                 resultSet.close();  
  97.                 preparedStatement.close();  
  98.                 conn.close();  
  99.             } catch (SQLException e) {                
  100.                 e.printStackTrace();  
  101.             }  
  102.               
  103.         }  
  104.     }  
  105.       
  106.     /** 
  107.      * 跟新数据库里的照片 
  108.      */  
  109.     public void updateImage(List<String> list){  
  110.         long start = System.currentTimeMillis();  
  111.          //加载驱动  
  112.         try {  
  113.         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
  114.   
  115.         //获得连接(目标库)  
  116.         Connection conn = DriverManager.getConnection(  
  117.                 propertiesUtil.getProperties(toUrl),propertiesUtil.getProperties(toUserName),propertiesUtil.getProperties(toPassword));  
  118.         PreparedStatement preparedStatement = conn.prepareStatement("update employee_info set photo=? where account=?");  
  119.          
  120.         PreparedStatement  preparedForEmpty = conn.prepareStatement("update EMPLOYEE_INFO t set photo=empty_blob()  where t.account=? ");  
  121.           
  122.         PreparedStatement  preparedForUpdate = conn.prepareStatement("select photo from employee_info  where account=? for update");  
  123.           
  124.         conn.setAutoCommit(false);  
  125.           
  126.         for(int i=0,size=list.size(); i<size; i++){  
  127.              File imgFile = new File("D:\\image\\"+list.get(i)+".jpg");  
  128.              //判断图片是否存在  
  129.              if(imgFile.exists()){  
  130.                  System.out.println("i:"+i+"account:"+list.get(i));   
  131.                 InputStream inputStream = new FileInputStream(imgFile);               
  132.                 //首先将照片设置为空               
  133.                 preparedForEmpty.setString(1, list.get(i));  
  134.                 preparedForEmpty.executeUpdate();  
  135.                   
  136.                 //查询Blob, 获得Blob的Cursor,一定注意sql后面有for update,                 
  137.                 preparedForUpdate.setString(1, list.get(i));  
  138.                 ResultSet rs= preparedForUpdate.executeQuery();  
  139.                 BLOB blob = null;  
  140.                 while(rs.next()){  
  141.                     blob = (BLOB)rs.getBlob(1);                   
  142.                     //使用字节流将待入库的文件写入到blob中  
  143.                     byte[] temp = new byte[inputStream.available()];     
  144.                     inputStream.read(temp);     
  145.                     OutputStream out = blob.getBinaryOutputStream();     
  146.                     out.write(temp);                         
  147.                     out.close();  
  148.                     inputStream.close();  
  149.                       
  150.                     //向数据库中写入数据  
  151.                     preparedStatement.setBlob(1,blob);  
  152.                     preparedStatement.setString(2, (String) list.get(i));                    
  153.                       
  154.                     preparedStatement.executeUpdate();  
  155.                     conn.commit();                    
  156.                 }                
  157.              }else{  
  158.                  System.out.println("找不到文件"+list.get(i));  
  159.                  continue;  
  160.              }         
  161.         }  
  162.          
  163.             long end = System.currentTimeMillis();  
  164.             preparedStatement.close();  
  165.             System.out.println("导入照片结束,耗时:"+((end-start)/1000/60)+"分钟");  
  166.         } catch (Exception e) {  
  167.             e.printStackTrace();  
  168.         }  
  169.     }  
  170.       
  171.     /** 
  172.      * 读取某个文件夹下的所有文件 
  173.      */  
  174.     public static List<String> readfile(String filepath) throws Exception {  
  175.         List name = new ArrayList<String>();      
  176.         try {  
  177.   
  178.                     File file = new File(filepath);  
  179.                     if (!file.isDirectory()) {  
  180.                             System.out.println("文件");  
  181.                             System.out.println("path=" + file.getPath());  
  182.                             System.out.println("absolutepath=" + file.getAbsolutePath());  
  183.                             System.out.println("name=" + file.getName());  
  184.   
  185.                     } else if (file.isDirectory()) {  
  186.                             System.out.println("文件夹");  
  187.                             String[] filelist = file.list();  
  188.                             for (int i = 0; i < filelist.length; i++) {  
  189.                                     File readfile = new File(filepath + "\\" + filelist[i]);  
  190.                                     if (!readfile.isDirectory()) {  
  191.                                             System.out.println("path=" + readfile.getPath());  
  192.                                             System.out.println("absolutepath="  
  193.                                                             + readfile.getAbsolutePath());  
  194.                                             System.out.println("name=" + readfile.getName());  
  195.                                               
  196.                                             //将文件名如:002125457.jpg的工号取出  
  197.                                             name.add(readfile.getName().substring(0, readfile.getName().lastIndexOf(".")));  
  198.   
  199.                                     } else if (readfile.isDirectory()) {  
  200.                                             readfile(filepath + "\\" + filelist[i]);  
  201.                                     }  
  202.                             }  
  203.   
  204.                     }  
  205.   
  206.             } catch (FileNotFoundException e) {  
  207.                     System.out.println("readfile()   Exception:" + e.getMessage());  
  208.             }  
  209.           
  210.             return name;  
  211.     }  
  212.   

转自:http://blog.csdn.net/u013341688/article/details/51832575

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值