上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)(2)

来源:互联网 发布:市场营销实训软件 编辑:程序博客网 时间:2024/06/05 18:06

首先说写入oracle的Blob字段

上一文章的实现方法没有贴出来,如下:

TempUserService.java

[html] view plaincopy
  1. package org.hd.tempuser.service;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5. import java.sql.Blob;  
  6. import java.sql.SQLException;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.hd.tempuser.model.TempUser;  
  11. import org.rd.framework.query.support.PaginationSupport;  
  12.   
  13. public interface TempUserService {  
  14.     public static final String BEAN_ID = "tempUserService";  
  15.       
  16.     public List getTempUserList(Map queryParams,PaginationSupport paginationSupport);  
  17.     public TempUser getTempUserById(String tempuserid);  
  18.     public void delTempUserById(String tempuserid);  
  19.       
  20.     public TempUser addTempUser(TempUser tempUser);  
  21.     public void editTempUser(TempUser tempUser);  
  22.       
  23.     public void addTempUserWriteBlob(TempUser tempUser,String url,String loginUserJobNum);  
  24.     public void editTempUserWriteBlob(TempUser tempUser,String url,String loginUserJobNum);  
  25.     public void editTempUserWithoutBlob(TempUser tempUser,String loginUserJobNum);  
  26.     public void clearPhotoBlob(TempUser tempUser);  
  27. }  


TempUserServiceImpl.java

 

[java] view plaincopy
  1. package org.hd.tempuser.service.impl;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.ByteArrayOutputStream;  
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileNotFoundException;  
  9. import java.io.IOException;  
  10. import java.io.InputStream;  
  11. import java.io.OutputStream;  
  12. import java.sql.Blob;  
  13. import java.sql.Connection;  
  14. import java.sql.DriverManager;  
  15. import java.sql.ResultSet;  
  16. import java.sql.SQLException;  
  17. import java.sql.Statement;  
  18. import java.util.Date;  
  19. import java.util.List;  
  20. import java.util.Map;  
  21.   
  22. import oracle.sql.BLOB;  
  23.   
  24. import org.hd.tempuser.model.TempUser;  
  25. import org.hd.tempuser.service.TempUserService;  
  26. import org.hd.util.BlobUtil;  
  27. import org.hibernate.Hibernate;  
  28. import org.hibernate.LockMode;  
  29. import org.hibernate.Session;  
  30. import org.hibernate.Transaction;  
  31. import org.hibernate.lob.SerializableBlob;  
  32. import org.rd.framework.common.container.ContainerManager;  
  33. import org.rd.framework.dao.impl.CommonServiceImpl;  
  34. import org.rd.framework.keygen.IKeyGen;  
  35. import org.rd.framework.query.support.PaginationSupport;  
  36. import org.rd.framework.util.WebUtils;  
  37.   
  38.   
  39. public class TempUserServiceImpl  extends CommonServiceImpl implements TempUserService{  
  40.   
  41.     @Override  
  42.     public List getTempUserList(Map queryParams,  
  43.             PaginationSupport paginationSupport) {  
  44.         String qualificationString = WebUtils.getQualificationString(queryParams);  
  45.         String queryString = "select queryby from TempUser queryby" + qualificationString;  
  46.         String countString = "select count(queryby.hduid) from TempUser queryby" + qualificationString;  
  47.         paginationSupport.setTotalCount(count(countString));  
  48.         return find(queryString, paginationSupport);  
  49.     }  
  50.       
  51.       
  52.     public TempUser getTempUserById(String tempuserid){  
  53.         TempUser tempUser=null;  
  54.         String queryString = "select queryby from TempUser queryby  where queryby.hduid='"   
  55.         + tempuserid.trim()+"'";  
  56.         List list=find(queryString);  
  57.         if(list.size()>0){  
  58.             tempUser=(TempUser)list.get(0);  
  59.         }  
  60.         return tempUser;  
  61.           
  62.     }  
  63.       
  64.     public void delTempUserById(String tempuserid){  
  65.         TempUser tempUser=null;  
  66.         String queryString = "delete from hd_user queryby  where queryby.hduid='"   
  67.         + tempuserid.trim()+"'";  
  68.         if(tempuserid==null||tempuserid.equals("")){  
  69.               
  70.         }else{  
  71.             createNativeSQL(queryString);  
  72.         }  
  73.           
  74.     }  
  75.   
  76.       
  77.     public TempUser addTempUser(TempUser tempUser){  
  78.         IKeyGen keyService = (IKeyGen) (IKeyGen) ContainerManager  
  79.                 .getComponent(IKeyGen.BEAN_ID);  
  80.         String Id = keyService.getUUIDKey();  
  81.         tempUser.setHduid(Id);  
  82.         //默认字段  
  83. //      tempUser.setCreate_date(new Date());  
  84. //      tempUser.setCreator(creator)  
  85.           
  86.         create(tempUser);  
  87.           
  88.         return tempUser;  
  89.     }  
  90.       
  91.     public void editTempUser(TempUser tempUser){  
  92.         this.update(tempUser);  
  93.     }  
  94.     //此方法作废  
  95.     public void addTempUserTt(TempUser tempUser,Blob hbblob,String pathh){  
  96.         String serverLocalPath=BlobUtil.WEBCONTENT_PATH+pathh.replaceAll("/""\\\\");  
  97.         IKeyGen keyService = (IKeyGen) (IKeyGen) ContainerManager  
  98.                 .getComponent(IKeyGen.BEAN_ID);  
  99.         String Id = keyService.getUUIDKey();  
  100.         tempUser.setHduid(Id);  
  101.           
  102.           
  103.         Session s = null;      
  104.         byte[] buffer = new byte[1];  
  105.         buffer[0] = 1;  
  106.         try {  
  107.           s = this.getSessionFactory().openSession();    
  108.           Transaction tx = s.beginTransaction();  
  109.           s.save(tempUser);  
  110.           s.flush();  
  111.           s.refresh(tempUser, LockMode.UPGRADE);     
  112.           BLOB blob = (BLOB)hbblob;     
  113.           OutputStream out = blob.getBinaryOutputStream();   
  114. //        String fileName = "oraclejdbc.jar";  
  115.           File f = new File(serverLocalPath);  
  116.           FileInputStream fin = new FileInputStream(f);     
  117.           int count = -1, total = 0;  
  118.           byte[] data = new byte[(int)fin.available()];  
  119.           fin.read(data);  
  120.           out.write(data);  
  121.           fin.close();  
  122.           out.close();  
  123.           s.flush();  
  124.           tx.commit();  
  125.          
  126.         } catch (Exception e){  
  127.           System.out.println(e.getMessage());  
  128.         } finally {  
  129.           if (s != null);  
  130.             try {  
  131.               s.close();;  
  132.             } catch (Exception e) {}  
  133.         }      
  134.           
  135.           
  136.     }  
  137.     //此方法作废  
  138.     public void addTempUserTtdd(TempUser tempUser,Blob hbblob,String pathh) throws SQLException, IOException, FileNotFoundException{  
  139.         String serverLocalPath=BlobUtil.WEBCONTENT_PATH+pathh.replaceAll("/""\\\\");  
  140.         IKeyGen keyService = (IKeyGen) (IKeyGen) ContainerManager  
  141.                 .getComponent(IKeyGen.BEAN_ID);  
  142.         String Id = keyService.getUUIDKey();  
  143.         tempUser.setHduid(Id);  
  144.           
  145.         //得到数据库连接(驱动包是weblogic的,没有下载任何新版本)  
  146.         Connection con=null;  
  147.         ResultSet rs=null;  
  148.           
  149.         InputStream in=null;  
  150.         BufferedInputStream bis=null;  
  151.           
  152.         OutputStream os=null;  
  153.         BufferedOutputStream bos=null;  
  154.         try {  
  155.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  156.                 con = DriverManager.getConnection(  
  157.                           "jdbc:oracle:thin:@10.23.117.149:1521:direct""helpdesk""helpdesk");  
  158.                 //处理事务  
  159.                 con.setAutoCommit(false);  
  160.                 Statement st = con.createStatement();  
  161. //              //插入一个空对象  
  162. //              st.executeUpdate("insert into BLOBIMG  values(103,empty_blob())");  
  163.                 String upsql="insert into hd_user(hduid,picture) values('"+Id+"',empty_blob())";  
  164.                 st.executeQuery(upsql);  
  165.                   
  166.                 //用for update方式锁定数据行  
  167.                 String sql="select picture from hd_user where hduid='"+tempUser.getHduid()+"' for update";  
  168.                 rs = st.executeQuery(sql);  
  169.                 System.out.println(sql);  
  170. //                  if (rs.next()) {  
  171.                            //得到java.sql.Blob对象,然后Cast为oracle.sql.BLOB  
  172.                 System.out.println(rs.getFetchSize());  
  173.                 rs.next();  
  174.                         oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);  
  175.                            //到数据库的输出流  
  176.                         os = blob.getBinaryOutputStream();  
  177.                         bos=new BufferedOutputStream(os);  
  178.                            //这里用一个文件模拟输入流  
  179.                         File file = new File(serverLocalPath);  
  180.                           in = new FileInputStream(file);  
  181.                           bis=new BufferedInputStream(new FileInputStream(file));  
  182.                         //将输入流写到输出流  
  183.                           byte[] b = new byte[1024*1024];  
  184.                          int len = 0;  
  185.                           while (-1!=(len=bis.read(b, 0, b.length))) {  
  186.                                   bos.write(b, 0, len);  
  187.                           }  
  188. //              }   
  189.               
  190.   
  191.         } catch (ClassNotFoundException e) {  
  192.             e.printStackTrace();  
  193.         } catch (IOException e) {  
  194.             // TODO Auto-generated catch block  
  195.             e.printStackTrace();  
  196.         }catch (SQLException e) {  
  197.             // TODO Auto-generated catch block  
  198.             e.printStackTrace();  
  199.         }finally{  
  200.   
  201.                if(os!=null){  
  202.                   // os.flush();  
  203.                    os.close();  
  204.                }  
  205.                if(bos!=null){  
  206.                   // bos.flush();  
  207.                    bos.close();  
  208.                }  
  209.                if(in!=null){  
  210.                    in.close();  
  211.                }  
  212.                if(bis!=null){  
  213.                    bis.close();  
  214.                }  
  215.                 
  216.                if(con!=null){  
  217.                    con.commit();  
  218.                    con.close();  
  219.                }  
  220.         }  
  221.           
  222.     }  
  223.     /** 
  224.      * 增加用户,把照片从临时的上传目录下写入数据库Blob 
  225.      */  
  226.     public void addTempUserWriteBlob(TempUser tempUser,String url,String loginUserJobNum) {  
  227.         String serverLocalPath=BlobUtil.WEBCONTENT_PATH+url.replaceAll("/""\\\\");  
  228.         IKeyGen keyService = (IKeyGen) (IKeyGen) ContainerManager  
  229.                 .getComponent(IKeyGen.BEAN_ID);  
  230.         String Id = keyService.getUUIDKey();  
  231.         tempUser.setHduid(Id);  
  232.         //默认字段  
  233.         tempUser.setCreate_date(new Date());  
  234.         tempUser.setCreator(loginUserJobNum);  
  235.         try {  
  236.             tempUser.setPicture(BLOB.empty_lob());  
  237.         } catch (SQLException e2) {  
  238.             e2.printStackTrace();  
  239.         }  
  240.           
  241.           
  242.         Session s=this.getSessionFactory().openSession();  
  243.         Transaction t = s.beginTransaction();  
  244.         s.save(tempUser);  
  245.         s.flush();  
  246.         s.refresh(tempUser,LockMode.UPGRADE);  //锁定为更新模式  
  247.         try{  
  248.             File uploadFile=new File(serverLocalPath);  
  249.             FileInputStream fis = new FileInputStream(uploadFile);     
  250.             ByteArrayOutputStream out = new ByteArrayOutputStream();     
  251.             byte[] b = new byte[1024];     
  252.             int n;     
  253.             while ((n=fis.read(b)) != -1) {     
  254.                  out.write(b,0,n);     
  255.             }     
  256.             fis.close();     
  257.             out.close();     
  258.             byte[] content = out.toByteArray();    
  259.             tempUser.setPicture(Hibernate.createBlob(content));  
  260.               
  261.               
  262.             s.flush();  
  263.             t.commit();  
  264.             s.close();  
  265.         }  
  266.         catch (FileNotFoundException e) {  
  267.         e.printStackTrace();  
  268.     } catch (IOException e) {  
  269.         e.printStackTrace();  
  270.     }  
  271.   
  272.     }  
  273.       
  274.     /** 
  275.      * 编辑用户,把照片从临时的上传目录下写入数据库Blob 
  276.      */  
  277.     public void editTempUserWriteBlob(TempUser tempUser,String url,String loginUserJobNum) {  
  278.         String serverLocalPath=BlobUtil.WEBCONTENT_PATH+url.replaceAll("/""\\\\");  
  279.         //默认字段  
  280.         tempUser.setCreate_date(new Date());  
  281.         tempUser.setCreator(loginUserJobNum);  
  282.         try {  
  283.             tempUser.setPicture(BLOB.empty_lob());  
  284.         } catch (SQLException e2) {  
  285.             e2.printStackTrace();  
  286.         }  
  287. //      String nasql="update  hd_user g  set g.picture=null  " +  
  288. //              " where g.hduid='"+tempUser.getHduid().trim()+"' ";  
  289. //      this.createNativeSQL(nasql);  
  290.           
  291.         Session s=this.getSessionFactory().openSession();  
  292.         Transaction t = s.beginTransaction();  
  293.           
  294.         tempUser.setPicture(Hibernate.createBlob(new byte[1]));  
  295.         s.saveOrUpdate(tempUser);  
  296.         s.flush();  
  297.         s.refresh(tempUser,LockMode.UPGRADE);  //锁定为更新模式  
  298.         try{  
  299.             File uploadFile=new File(serverLocalPath);  
  300.             FileInputStream fis = new FileInputStream(uploadFile);     
  301.             ByteArrayOutputStream out = new ByteArrayOutputStream();     
  302.             byte[] b = new byte[1024];     
  303.             int n;     
  304.             while ((n=fis.read(b)) != -1) {     
  305.                  out.write(b,0,n);     
  306.             }     
  307.             fis.close();     
  308.             out.close();     
  309.             byte[] content = out.toByteArray();    
  310.             tempUser.setPicture(Hibernate.createBlob(content));  
  311.               
  312.               
  313.             s.flush();  
  314.             t.commit();  
  315.             s.close();  
  316.         }  
  317.         catch (FileNotFoundException e) {  
  318.         e.printStackTrace();  
  319.     } catch (IOException e) {  
  320.         e.printStackTrace();  
  321.     }  
  322.   
  323.     }  
  324.       
  325.     public void clearPhotoBlob(TempUser tempUser){  
  326.         String nasql="update  hd_user g  set g.picture=null  " +  
  327.                 " where g.hduid='"+tempUser.getHduid().trim()+"' ";  
  328.         this.createNativeSQL(nasql);  
  329.     }  
  330.       
  331.     /** 
  332.      * 编辑用户,不修改照片 
  333.      */  
  334.     public void editTempUserWithoutBlob(TempUser tempUser,String loginUserJobNum) {  
  335.         /* 
  336. update hd_user t  
  337.  set t.uname='q',  
  338.  t.upass='q', 
  339.  t.realname='娜美', 
  340.  t.sex='女', 
  341.  t.email='tt@kdk.com', 
  342.  t.cellphone='45345345', 
  343.  t.ext='123', 
  344.  t.phone='321', 
  345.  t.job_level='坐席', 
  346.  t.edit_date=sysdate, 
  347.  t.editor='suolong', 
  348.  t.remark='editsuccess'  
  349.  where t.hduid='F15E73BD0A18084A01AFB22A54946F6F' 
  350.   
  351.   
  352.          */  
  353.         String hduid=tempUser.getHduid();  
  354.         if(hduid==null||hduid.trim().equals("")){  
  355.               
  356.         }else{  
  357.             hduid=hduid.trim();  
  358.             String sql=" update hd_user t  " +  
  359.                     "  set t.uname='"+tempUser.getUname().trim()+"', " +  
  360.                     "  t.upass='"+tempUser.getUpass().trim()+"', " +  
  361.                     "  t.realname='"+tempUser.getRealname()+"', " +  
  362.                     "  t.sex='"+tempUser.getSex().trim()+"', " +  
  363.                     "  t.email='"+tempUser.getEmail()+"', " +  
  364.                     "  t.cellphone='"+tempUser.getCellphone()+"', " +  
  365.                     "  t.ext='"+tempUser.getExt()+"', " +  
  366.                     "  t.phone='"+tempUser.getPhone()+"', " +  
  367.                     "  t.job_level='"+tempUser.getJob_level()+"', " +  
  368.                     "  t.edit_date=sysdate, " +  
  369.                     "  t.editor='"+loginUserJobNum+"', " +  
  370.                     "  t.remark='"+tempUser.getRemark()+"'  " +  
  371.                     "  where t.hduid='"+hduid+"' ";  
  372.               
  373.             this.createNativeSQL(sql);  
  374.         }  
  375.           
  376.           
  377.     }  
  378. }  


 

BlobUtil.java

[java] view plaincopy
  1. package org.hd.util;  
  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.IOException;  
  8. import java.io.InputStream;  
  9. import java.sql.Blob;  
  10. import java.sql.SQLException;  
  11.   
  12. import org.hibernate.Hibernate;  
  13.   
  14. public class BlobUtil {  
  15.       
  16.     public static String WEBCONTENT_PATH="";  
  17.     public static String BASE_PATH="";  
  18.     public static String ODSUSER_PHOTO_PATH="";//ods用户头像目录  
  19.     public static String TEMPUSER_PHOTO_PATH="";//临时长期用户头像目录  
  20.     public static String TEMP_UPLOAD_PATH="";//上传时的临时目录  
  21.       
  22.     public static String DirTypeTempupload="tempupload";  
  23.     public static String DirTypeOdsuser="odsuser";  
  24.     public static String DirTypeTempuser="tempuser";  
  25.     static{  
  26.   
  27.         String clapath=BlobUtil.class.getResource("/").getPath();  
  28.         File claFile=new File(clapath);  
  29.         String basePath=claFile.getParentFile().getParentFile().getPath();  
  30.         WEBCONTENT_PATH=basePath;  
  31.         BASE_PATH=basePath+"\\tempphoto";  
  32.         File baseFile=new File(BASE_PATH);  
  33.         if(!baseFile.exists()){  
  34.             baseFile.mkdir();  
  35.         }  
  36.           
  37.         ODSUSER_PHOTO_PATH=BASE_PATH+"\\odsuser";  
  38.         TEMPUSER_PHOTO_PATH=BASE_PATH+"\\tempuser";  
  39.         TEMP_UPLOAD_PATH=BASE_PATH+"\\tempupload";  
  40.           
  41.         File odsFile=new File(ODSUSER_PHOTO_PATH);  
  42.         File tempFile=new File(TEMPUSER_PHOTO_PATH);  
  43.         File tempUpload=new File(TEMP_UPLOAD_PATH);  
  44.         if(!odsFile.exists()){  
  45.             odsFile.mkdir();  
  46.         }  
  47.         if(!tempFile.exists()){  
  48.             tempFile.mkdir();  
  49.         }  
  50.         if(!tempUpload.exists()){  
  51.             tempUpload.mkdir();  
  52.         }  
  53.         System.out.println(odsFile.getPath()+"---"+odsFile.exists());  
  54.         System.out.println(tempFile.getPath()+"---"+tempFile.exists());  
  55.         System.out.println(tempUpload.getPath()+"---"+tempUpload.exists());  
  56.       
  57.     }  
  58.       
  59.     public static String makeLocalPhoto(Blob blob,String tergetDirName,boolean isTempUser){  
  60.         String relativepath="";  
  61.         tergetDirName=tergetDirName.trim();  
  62.         if(blob==null){  
  63.             System.out.println("无法读取图片信息!");  
  64.         }else{  
  65.             if(tergetDirName==null||tergetDirName.trim().equals("")){  
  66.                 System.out.println("无法获取工号创建目录!");  
  67.             }else{  
  68.                 if(isTempUser){//临时长期用户  
  69.                     File tempFile=new File(BlobUtil.TEMPUSER_PHOTO_PATH+"\\"+tergetDirName);  
  70.                     if(!tempFile.exists()){  
  71.                         tempFile.mkdir();  
  72.                     }  
  73.                     boolean falg=blobToPhoto(blob,tempFile,tergetDirName);  
  74.                     if(falg){  
  75.                         relativepath="/tempphoto/tempuser/"+tergetDirName+"/"+tergetDirName+".jpg";  
  76.                     }else{  
  77.                     }  
  78.                 }else{//ods用户  
  79.                     File odsFile=new File(BlobUtil.ODSUSER_PHOTO_PATH+"\\"+tergetDirName);  
  80.                     if(!odsFile.exists()){  
  81.                         odsFile.mkdir();  
  82.                     }  
  83.                     boolean falg=blobToPhoto(blob,odsFile,tergetDirName);  
  84.                     if(falg){  
  85.                         relativepath="/tempphoto/odsuser/"+tergetDirName+"/"+tergetDirName+".jpg";  
  86.                     }else{  
  87.                     }  
  88.                 }  
  89.             }  
  90.         }//end else  
  91.           
  92.         return relativepath;  
  93.     }  
  94.       
  95.     public static boolean blobToPhoto(Blob blob,File tergetDirFile,String tergetDirName){  
  96.         String photoPath=tergetDirFile.getPath().trim();  
  97.         boolean issuccess=true;  
  98.         if(!tergetDirFile.exists()){  
  99.             System.out.println("目标路径不存在"+tergetDirFile.getPath());  
  100.             issuccess=false;  
  101.         }else if(!tergetDirFile.isDirectory()){  
  102.             System.out.println("目标路径不是目录"+tergetDirFile.getPath());  
  103.             issuccess=false;  
  104.         }else{  
  105.             File photo=new File(photoPath+"\\"+tergetDirName.trim()+".jpg");  
  106.             FileOutputStream fos = null;  
  107.             InputStream is = null;  
  108.             byte[] Buffer = new byte[4096];  
  109.             try {  
  110.                 is=blob.getBinaryStream();  
  111.                 fos=new FileOutputStream(photo);  
  112.                 int size = 0;  
  113.                 while ((size = is.read(Buffer)) != -1) {  
  114.                     fos.write(Buffer, 0, size);  
  115.                 }  
  116.             } catch (SQLException e) {  
  117.                 e.printStackTrace();  
  118.             }  catch (FileNotFoundException e) {  
  119.                 e.printStackTrace();  
  120.             } catch (IOException e) {  
  121.                 // TODO Auto-generated catch block  
  122.                 e.printStackTrace();  
  123.             }catch (Exception e) {  
  124.                 // TODO Auto-generated catch block  
  125.                 e.printStackTrace();  
  126.             } finally {  
  127.                 try {  
  128.                     if (fos != null) {  
  129.                         fos.flush();  
  130.                         fos.close();  
  131.                     }  
  132.                     if (is != null) {  
  133.                         is.close();  
  134.                     }  
  135.                 } catch (IOException e) {  
  136.                     e.printStackTrace();  
  137.                 }  
  138.   
  139.             }  
  140.   
  141.         }//end else  
  142.           
  143.         return issuccess;  
  144.     }  
  145.     public static Blob photoToBlob(String filePath){  
  146.         Blob blob=null;  
  147.         FileInputStream fis = null;  
  148.           
  149.         String serverLocalPath=BlobUtil.WEBCONTENT_PATH+filePath.replaceAll("/""\\\\");  
  150.         System.out.println("serverLocalPath::"+serverLocalPath);  
  151.         File file=new File(serverLocalPath);  
  152.         try {  
  153.             fis=new FileInputStream(file);  
  154.             blob=Hibernate.createBlob(fis);  
  155.         }catch (FileNotFoundException e) {  
  156.             e.printStackTrace();  
  157.         } catch (IOException e) {  
  158.             e.printStackTrace();  
  159.         }catch (Exception e) {  
  160.             e.printStackTrace();  
  161.         } finally {  
  162.             try {  
  163.                 if (fis != null) {  
  164.                     fis.close();  
  165.                 }  
  166.             } catch (IOException e) {  
  167.                 e.printStackTrace();  
  168.             }  
  169.         }  
  170.         return blob;  
  171.     }  
  172.       
  173.     public static String getUploadTempPhotoUrl(String photoUrl,String jobNumber){  
  174.         String webUrl="";  
  175.         String toUrl="";  
  176.         File tempUploadFile=new File(BlobUtil.TEMP_UPLOAD_PATH+"\\"+jobNumber);  
  177.         if(!tempUploadFile.exists()){  
  178.             tempUploadFile.mkdir();  
  179.         }  
  180.         boolean falg=BlobUtil.isRightFileSuffix(photoUrl);  
  181.         if(falg){  
  182.             toUrl=BlobUtil.TEMP_UPLOAD_PATH+"\\"+jobNumber+"\\"+jobNumber+BlobUtil.getFileSuffix(photoUrl);  
  183.             BlobUtil.copyFile(photoUrl, toUrl);  
  184.             webUrl="/tempphoto/tempupload/"+jobNumber+"/"+jobNumber+BlobUtil.getFileSuffix(photoUrl);  
  185.               
  186.         }else{  
  187.         }  
  188.           
  189.           
  190.           
  191.         return webUrl;  
  192.     }  
  193.     //得到文件url文件的后缀  
  194.     public static String getFileSuffix(String fileUrl){  
  195.         String res="";  
  196.         int a=fileUrl.lastIndexOf(".");  
  197.         if(a>0){  
  198.             res=fileUrl.substring(a);  
  199.         }  
  200.         return res;  
  201.     }  
  202.     public static boolean isRightFileSuffix(String fileUrl){  
  203.         boolean flag=false;  
  204.         int a=fileUrl.lastIndexOf(".");  
  205.         if(a>0){  
  206.             flag=true;  
  207.         }  
  208.         return flag;  
  209.     }  
  210.       
  211.     public static void copyFile(String fromUrl,String toUrl){  
  212.         File fromFile=new File(fromUrl);  
  213.         File toFile=new File(toUrl);  
  214.         if(toFile.exists()){  
  215.             toFile.delete();  
  216.         }  
  217.           
  218.         FileOutputStream fos = null;  
  219.         FileInputStream fis = null;  
  220.         byte[] Buffer = new byte[4096];  
  221.         try {  
  222.             fis=new FileInputStream(fromFile);  
  223.             fos=new FileOutputStream(toFile);  
  224.             int size = 0;  
  225.             while ((size = fis.read(Buffer)) != -1) {  
  226.                 fos.write(Buffer, 0, size);  
  227.             }  
  228.         }catch (FileNotFoundException e) {  
  229.             e.printStackTrace();  
  230.         } catch (IOException e) {  
  231.             // TODO Auto-generated catch block  
  232.             e.printStackTrace();  
  233.         }catch (Exception e) {  
  234.             // TODO Auto-generated catch block  
  235.             e.printStackTrace();  
  236.         } finally {  
  237.             try {  
  238.                 if (fos != null) {  
  239.                     fos.flush();  
  240.                     fos.close();  
  241.                 }  
  242.                 if (fis != null) {  
  243.                     fis.close();  
  244.                 }  
  245.             } catch (IOException e) {  
  246.                 e.printStackTrace();  
  247.             }  
  248.   
  249.         }  
  250.   
  251.       
  252.     }  
  253.       
  254.     public static String makeDirByParamForOds(String jobNumberold,String type){  
  255.         String jobNumber=jobNumberold.trim();  
  256.         String rePath="";  
  257.         if(jobNumber==null||jobNumber.trim().equals("")){  
  258.               
  259.         }else{  
  260.             if(type==BlobUtil.DirTypeTempupload){  
  261.                 File tempUploadFile=new File(BlobUtil.TEMP_UPLOAD_PATH+"\\"+jobNumber);  
  262.                 if(!tempUploadFile.exists()){  
  263.                     tempUploadFile.mkdir();  
  264.                 }  
  265.                 rePath="/tempupload/"+jobNumber+"/"+jobNumber;  
  266.             }else if(type==BlobUtil.DirTypeOdsuser){  
  267.                 File tempUploadFile=new File(BlobUtil.ODSUSER_PHOTO_PATH+"\\"+jobNumber);  
  268.                 if(!tempUploadFile.exists()){  
  269.                     tempUploadFile.mkdir();  
  270.                 }  
  271.                 rePath="/odsuser/"+jobNumber+"/"+jobNumber;  
  272.             }else if(type==BlobUtil.DirTypeTempuser){  
  273.                 File tempUploadFile=new File(BlobUtil.TEMPUSER_PHOTO_PATH+"\\"+jobNumber);  
  274.                 if(!tempUploadFile.exists()){  
  275.                     tempUploadFile.mkdir();  
  276.                 }  
  277.                 rePath="/tempuser/"+jobNumber+"/"+jobNumber;  
  278.             }  
  279.         }  
  280.         return rePath;  
  281.     }  
  282.       
  283.     public static String makeDirByParamForOdsRealPath(String jobNumberold,String type){  
  284.         String jobNumber=jobNumberold.trim();  
  285.         String rePath="";  
  286.         if(jobNumber==null||jobNumber.trim().equals("")){  
  287.               
  288.         }else{  
  289.             if(type==BlobUtil.DirTypeTempupload){  
  290.                 File tempUploadFile=new File(BlobUtil.TEMP_UPLOAD_PATH+"\\"+jobNumber);  
  291.                 if(!tempUploadFile.exists()){  
  292.                     tempUploadFile.mkdir();  
  293.                 }  
  294.                 rePath="\\\\tempupload\\\\"+jobNumber+"\\\\"+jobNumber;  
  295.             }else if(type==BlobUtil.DirTypeOdsuser){  
  296.                 File tempUploadFile=new File(BlobUtil.ODSUSER_PHOTO_PATH+"\\"+jobNumber);  
  297.                 if(!tempUploadFile.exists()){  
  298.                     tempUploadFile.mkdir();  
  299.                 }  
  300.                 rePath="\\\\odsuser\\\\"+jobNumber+"\\\\"+jobNumber;  
  301.             }else if(type==BlobUtil.DirTypeTempuser){  
  302.                 File tempUploadFile=new File(BlobUtil.TEMPUSER_PHOTO_PATH+"\\"+jobNumber);  
  303.                 if(!tempUploadFile.exists()){  
  304.                     tempUploadFile.mkdir();  
  305.                 }  
  306.                 rePath="\\\\tempuser\\\\"+jobNumber+"\\\\"+jobNumber;  
  307.             }  
  308.         }  
  309.         return rePath;  
  310.     }  
  311.       
  312.     public static String changeFileName(String fullold,String filename,String newname){  
  313.         String res="";  
  314.           
  315.         String a=filename.substring(0,filename.lastIndexOf("."));  
  316.         String b=BlobUtil.getFileSuffix(filename);  
  317.         res=fullold.replaceAll(filename, newname+b);  
  318.         return res;  
  319.           
  320.     }  
  321.     public static void main(String[] ss){  
  322.          String serverLocalPath=BlobUtil.BASE_PATH+"/gg/gg.jpg".replaceAll("/""\\\\");  
  323.             System.out.println("BASE_PATH::"+BlobUtil.BASE_PATH);  
  324.             System.out.println("WEBCONTENT_PATH::"+BlobUtil.WEBCONTENT_PATH);  
  325.             System.out.println("TEMP_UPLOAD_PATH::"+BlobUtil.TEMP_UPLOAD_PATH);  
  326.             System.out.println("ODSUSER_PHOTO_PATH::"+BlobUtil.ODSUSER_PHOTO_PATH);  
  327.             System.out.println("TEMPUSER_PHOTO_PATH::"+BlobUtil.TEMPUSER_PHOTO_PATH);  
  328.               
  329.             String aa="sfs.333.jpgg";  
  330.             System.out.println(aa.substring(0,aa.lastIndexOf(".")));  
  331.             System.out.println(BlobUtil.changeFileName("as/asd/3.jpg","3.jpg","nn"));  
  332.             /* 
  333. BASE_PATH::D:\HelpDeskProject\helpdesk-workSpace\HelpDesk\WebContent\tempphoto 
  334. WEBCONTENT_PATH::D:\HelpDeskProject\helpdesk-workSpace\HelpDesk\WebContent 
  335. TEMP_UPLOAD_PATH::D:\HelpDeskProject\helpdesk-workSpace\HelpDesk\WebContent\tempphoto\tempupload 
  336. ODSUSER_PHOTO_PATH::D:\HelpDeskProject\helpdesk-workSpace\HelpDesk\WebContent\tempphoto\odsuser 
  337. TEMPUSER_PHOTO_PATH::D:\HelpDeskProject\helpdesk-workSpace\HelpDesk\WebContent\tempphoto\tempuser 
  338.  
  339.              */  
  340.     }  
  341.   
  342. }  


 


TempUser.java

[java] view plaincopy
  1. package org.hd.tempuser.model;  
  2.   
  3. import java.util.Date;  
  4. import java.sql.Blob;  
  5.   
  6. public class TempUser {  
  7.     //import oracle.sql.BLOB;  
  8.       
  9.   
  10.     private String hduid;  
  11.     private String uname;  
  12.     private String upass;  
  13.     private String realname;  
  14.     private String sex;  
  15.   
  16.     private String email;  
  17.     private String utype;  
  18.     private String cellphone;  
  19.     private String ext;  
  20.     private String phone;  
  21.   
  22.     private String job_level;  
  23.     private String valid;  
  24.     private Blob picture;  
  25.     private Date create_date;  
  26.     private String creator;  
  27.   
  28.     private Date edit_date;  
  29.     private String editor;  
  30.     private String remark;  
  31.       
  32.     //虚拟字段   
  33.     private String tempUrl;//照片url  
  34.     private String vjob_level;//职级  
  35.       
  36.     public String getHduid() {  
  37.         return hduid;  
  38.     }  
  39.     public void setHduid(String hduid) {  
  40.         this.hduid = hduid;  
  41.     }  
  42.     public String getUname() {  
  43.         return uname;  
  44.     }  
  45.     public void setUname(String uname) {  
  46.         this.uname = uname;  
  47.     }  
  48.     public String getUpass() {  
  49.         return upass;  
  50.     }  
  51.     public void setUpass(String upass) {  
  52.         this.upass = upass;  
  53.     }  
  54.     public String getRealname() {  
  55.         return realname;  
  56.     }  
  57.     public void setRealname(String realname) {  
  58.         this.realname = realname;  
  59.     }  
  60.     public String getSex() {  
  61.         return sex;  
  62.     }  
  63.     public void setSex(String sex) {  
  64.         this.sex = sex;  
  65.     }  
  66.     public String getEmail() {  
  67.         return email;  
  68.     }  
  69.     public void setEmail(String email) {  
  70.         this.email = email;  
  71.     }  
  72.     public String getUtype() {  
  73.         return utype;  
  74.     }  
  75.     public void setUtype(String utype) {  
  76.         this.utype = utype;  
  77.     }  
  78.     public String getCellphone() {  
  79.         return cellphone;  
  80.     }  
  81.     public void setCellphone(String cellphone) {  
  82.         this.cellphone = cellphone;  
  83.     }  
  84.     public String getExt() {  
  85.         return ext;  
  86.     }  
  87.     public void setExt(String ext) {  
  88.         this.ext = ext;  
  89.     }  
  90.     public String getPhone() {  
  91.         return phone;  
  92.     }  
  93.     public void setPhone(String phone) {  
  94.         this.phone = phone;  
  95.     }  
  96.     public String getJob_level() {  
  97.         return job_level;  
  98.     }  
  99.     public void setJob_level(String job_level) {  
  100.         this.job_level = job_level;  
  101.     }  
  102.     public String getValid() {  
  103.         return valid;  
  104.     }  
  105.     public void setValid(String valid) {  
  106.         this.valid = valid;  
  107.     }  
  108.     public Blob getPicture() {  
  109.         return picture;  
  110.     }  
  111.     public void setPicture(Blob picture) {  
  112.         this.picture = picture;  
  113.     }  
  114.     public Date getCreate_date() {  
  115.         return create_date;  
  116.     }  
  117.     public void setCreate_date(Date create_date) {  
  118.         this.create_date = create_date;  
  119.     }  
  120.     public String getCreator() {  
  121.         return creator;  
  122.     }  
  123.     public void setCreator(String creator) {  
  124.         this.creator = creator;  
  125.     }  
  126.     public Date getEdit_date() {  
  127.         return edit_date;  
  128.     }  
  129.     public void setEdit_date(Date edit_date) {  
  130.         this.edit_date = edit_date;  
  131.     }  
  132.     public String getEditor() {  
  133.         return editor;  
  134.     }  
  135.     public void setEditor(String editor) {  
  136.         this.editor = editor;  
  137.     }  
  138.     public String getRemark() {  
  139.         return remark;  
  140.     }  
  141.     public void setRemark(String remark) {  
  142.         this.remark = remark;  
  143.     }  
  144.     public String getTempUrl() {  
  145.         return tempUrl;  
  146.     }  
  147.     public void setTempUrl(String tempUrl) {  
  148.         this.tempUrl = tempUrl;  
  149.     }  
  150.     public String getVjob_level() {  
  151.         return vjob_level;  
  152.     }  
  153.     public void setVjob_level(String vjob_level) {  
  154.         this.vjob_level = vjob_level;  
  155.     }  
  156.       
  157. }  


TempUser.hbm.xml

[java] view plaincopy
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping>  
  7.         <class name="org.hd.tempuser.model.TempUser" table="hd_user">  
  8.         <id name="hduid" type="java.lang.String">  
  9.             <column name="hduid" length="32" />  
  10.             <generator class="assigned" />  
  11.         </id>  
  12.         <property name="uname" type="java.lang.String">  
  13.             <column name="uname" length="50">  
  14.             </column>  
  15.         </property>  
  16.         <property name="upass" type="java.lang.String">  
  17.             <column name="upass" length="100" />  
  18.         </property>  
  19.         <property name="realname" type="java.lang.String">  
  20.             <column name="realname" length="50">  
  21.             </column>  
  22.         </property>  
  23.         <property name="sex" type="java.lang.String">  
  24.             <column name="sex" length="50" />  
  25.         </property>  
  26.           
  27.           
  28.         <property name="email" type="java.lang.String">  
  29.             <column name="email" length="100">  
  30.             </column>  
  31.         </property>  
  32.         <property name="utype" type="java.lang.String">  
  33.             <column name="utype" length="10" />  
  34.         </property>  
  35.         <property name="cellphone" type="java.lang.String">  
  36.             <column name="cellphone" length="50">  
  37.             </column>  
  38.         </property>  
  39.         <property name="ext" type="java.lang.String">  
  40.             <column name="ext" length="50" />  
  41.         </property>  
  42.         <property name="phone" type="java.lang.String">  
  43.             <column name="phone" length="50" />  
  44.         </property>  
  45.           
  46.           
  47.         <property name="job_level" type="java.lang.String">  
  48.             <column name="job_level" length="50" />  
  49.         </property>  
  50.         <property name="valid" type="java.lang.String">  
  51.             <column name="valid" length="10" />  
  52.         </property>  
  53.         <property name="picture" type="java.sql.Blob">  
  54.             <column name="picture" />  
  55.         </property>  
  56.         <property name="create_date" type="java.util.Date">  
  57.             <column name="create_date" />  
  58.         </property>  
  59.         <property name="creator" type="java.lang.String">  
  60.             <column name="creator" length="32" />  
  61.         </property>  
  62.           
  63.           
  64.         <property name="edit_date" type="java.util.Date">  
  65.             <column name="edit_date" />  
  66.         </property>  
  67.         <property name="editor" type="java.lang.String">  
  68.             <column name="editor" length="32" />  
  69.         </property>  
  70.         <property name="remark" type="java.lang.String">  
  71.             <column name="remark" length="200" />  
  72.         </property>  
  73.     </class>  
  74. </hibernate-mapping>  


picture是Blob字段(照片)

从上面的代码中说说核心代码;

写入Blob。

在新增时,picture首先需要赋值一个空的值,这个空值不是null  !!!而是BLOB.empty_lob()或者Hibernate.createBlob(new byte[1])

这样才算是符合Blob类型。

session需要改变更新模式 s.refresh(tempUser,LockMode.UPGRADE);  //锁定为更新模式

即for update.

这是才可以写入,二写入的内容是byte[]类型!

[java] view plaincopy
  1. /** 
  2.  * 增加用户,把照片从临时的上传目录下写入数据库Blob 
  3.  */  
  4. public void addTempUserWriteBlob(TempUser tempUser,String url,String loginUserJobNum) {  
  5.     String serverLocalPath=BlobUtil.WEBCONTENT_PATH+url.replaceAll("/""\\\\");  
  6.     IKeyGen keyService = (IKeyGen) (IKeyGen) ContainerManager  
  7.             .getComponent(IKeyGen.BEAN_ID);  
  8.     String Id = keyService.getUUIDKey();  
  9.     tempUser.setHduid(Id);  
  10.     //默认字段  
  11.     tempUser.setCreate_date(new Date());  
  12.     tempUser.setCreator(loginUserJobNum);  
  13.     try {  
  14.         tempUser.setPicture(BLOB.empty_lob());  
  15.     } catch (SQLException e2) {  
  16.         e2.printStackTrace();  
  17.     }  
  18.       
  19.       
  20.     Session s=this.getSessionFactory().openSession();  
  21.     Transaction t = s.beginTransaction();  
  22.     s.save(tempUser);  
  23.     s.flush();  
  24.     s.refresh(tempUser,LockMode.UPGRADE);  //锁定为更新模式  
  25.     try{  
  26.         File uploadFile=new File(serverLocalPath);  
  27.         FileInputStream fis = new FileInputStream(uploadFile);     
  28.         ByteArrayOutputStream out = new ByteArrayOutputStream();     
  29.         byte[] b = new byte[1024];     
  30.         int n;     
  31.         while ((n=fis.read(b)) != -1) {     
  32.              out.write(b,0,n);     
  33.         }     
  34.         fis.close();     
  35.         out.close();     
  36.         byte[] content = out.toByteArray();    
  37.         tempUser.setPicture(Hibernate.createBlob(content));  
  38.           
  39.           
  40.         s.flush();  
  41.         t.commit();  
  42.         s.close();  
  43.     }  
  44.     catch (FileNotFoundException e) {  
  45.     e.printStackTrace();  
  46. catch (IOException e) {  
  47.     e.printStackTrace();  
  48. }  
  49.   
  50. }  

 

 

编辑用户时,需要先把数据库的Blob字段置空null,然后再更新(不知有没有其他直接更新的方法)

所以在action我先tempUserService.clearPhotoBlob(getEditTempUser());

 

[java] view plaincopy
  1. public void clearPhotoBlob(TempUser tempUser){  
  2.     String nasql="update  hd_user g  set g.picture=null  " +  
  3.             " where g.hduid='"+tempUser.getHduid().trim()+"' ";  
  4.     this.createNativeSQL(nasql);  
  5. }  


然后再更新blob,

tempUserService.editTempUserWriteBlob(getEditTempUser(),
          getLocalPhotoUrl(), loginUserJobNum);

 

[java] view plaincopy
  1.     /** 
  2.      * 编辑用户,把照片从临时的上传目录下写入数据库Blob 
  3.      */  
  4.     public void editTempUserWriteBlob(TempUser tempUser,String url,String loginUserJobNum) {  
  5.         String serverLocalPath=BlobUtil.WEBCONTENT_PATH+url.replaceAll("/""\\\\");  
  6.         //默认字段  
  7.         tempUser.setCreate_date(new Date());  
  8.         tempUser.setCreator(loginUserJobNum);  
  9.         try {  
  10.             tempUser.setPicture(BLOB.empty_lob());  
  11.         } catch (SQLException e2) {  
  12.             e2.printStackTrace();  
  13.         }  
  14. //      String nasql="update  hd_user g  set g.picture=null  " +  
  15. //              " where g.hduid='"+tempUser.getHduid().trim()+"' ";  
  16. //      this.createNativeSQL(nasql);  
  17.           
  18.         Session s=this.getSessionFactory().openSession();  
  19.         Transaction t = s.beginTransaction();  
  20.           
  21.         tempUser.setPicture(Hibernate.createBlob(new byte[1]));  
  22.         s.saveOrUpdate(tempUser);  
  23.         s.flush();  
  24.         s.refresh(tempUser,LockMode.UPGRADE);  //锁定为更新模式  
  25.         try{  
  26.             File uploadFile=new File(serverLocalPath);  
  27.             FileInputStream fis = new FileInputStream(uploadFile);     
  28.             ByteArrayOutputStream out = new ByteArrayOutputStream();     
  29.             byte[] b = new byte[1024];     
  30.             int n;     
  31.             while ((n=fis.read(b)) != -1) {     
  32.                  out.write(b,0,n);     
  33.             }     
  34.             fis.close();     
  35.             out.close();     
  36.             byte[] content = out.toByteArray();    
  37.             tempUser.setPicture(Hibernate.createBlob(content));  
  38.               
  39.               
  40.             s.flush();  
  41.             t.commit();  
  42.             s.close();  
  43.         }  
  44.         catch (FileNotFoundException e) {  
  45.         e.printStackTrace();  
  46.     } catch (IOException e) {  
  47.         e.printStackTrace();  
  48.     }  
  49.   
  50.     }  

 

至于读取Blob,你只需要先读取出来放到服务器下某个路径,然后返回这个路径用于页面展示。

例如,在查看用户信息的时候,需要展示照片。

[java] view plaincopy
  1. //读取TempUser照片信息  
  2. public String getTempPhoto(TempUser tempUser){  
  3.     String resPath="";  
  4.     if(tempUser==null){  
  5.           
  6.     }else if(tempUser.getPicture()==null){  
  7.           
  8.     }else{  
  9.         Blob blob=(Blob)tempUser.getPicture();  
  10.         resPath=BlobUtil.makeLocalPhoto(blob, tempUser.getUname(), true);  
  11.           
  12.     }//end else  
  13.       
  14.     return resPath;  
  15. }  


其中

[java] view plaincopy
  1. public static String makeLocalPhoto(Blob blob,String tergetDirName,boolean isTempUser){  
  2.     String relativepath="";  
  3.     tergetDirName=tergetDirName.trim();  
  4.     if(blob==null){  
  5.         System.out.println("无法读取图片信息!");  
  6.     }else{  
  7.         if(tergetDirName==null||tergetDirName.trim().equals("")){  
  8.             System.out.println("无法获取工号创建目录!");  
  9.         }else{  
  10.             if(isTempUser){//临时长期用户  
  11.                 File tempFile=new File(BlobUtil.TEMPUSER_PHOTO_PATH+"\\"+tergetDirName);  
  12.                 if(!tempFile.exists()){  
  13.                     tempFile.mkdir();  
  14.                 }  
  15.                 boolean falg=blobToPhoto(blob,tempFile,tergetDirName);  
  16.                 if(falg){  
  17.                     relativepath="/tempphoto/tempuser/"+tergetDirName+"/"+tergetDirName+".jpg";  
  18.                 }else{  
  19.                 }  
  20.             }else{//ods用户  
  21.                 File odsFile=new File(BlobUtil.ODSUSER_PHOTO_PATH+"\\"+tergetDirName);  
  22.                 if(!odsFile.exists()){  
  23.                     odsFile.mkdir();  
  24.                 }  
  25.                 boolean falg=blobToPhoto(blob,odsFile,tergetDirName);  
  26.                 if(falg){  
  27.                     relativepath="/tempphoto/odsuser/"+tergetDirName+"/"+tergetDirName+".jpg";  
  28.                 }else{  
  29.                 }  
  30.             }  
  31.         }  
  32.     }//end else  
  33.       
  34.     return relativepath;  
  35. }  


其中

[java] view plaincopy
  1. public static boolean blobToPhoto(Blob blob,File tergetDirFile,String tergetDirName){  
  2.     String photoPath=tergetDirFile.getPath().trim();  
  3.     boolean issuccess=true;  
  4.     if(!tergetDirFile.exists()){  
  5.         System.out.println("目标路径不存在"+tergetDirFile.getPath());  
  6.         issuccess=false;  
  7.     }else if(!tergetDirFile.isDirectory()){  
  8.         System.out.println("目标路径不是目录"+tergetDirFile.getPath());  
  9.         issuccess=false;  
  10.     }else{  
  11.         File photo=new File(photoPath+"\\"+tergetDirName.trim()+".jpg");  
  12.         FileOutputStream fos = null;  
  13.         InputStream is = null;  
  14.         byte[] Buffer = new byte[4096];  
  15.         try {  
  16.             is=blob.getBinaryStream();  
  17.             fos=new FileOutputStream(photo);  
  18.             int size = 0;  
  19.             while ((size = is.read(Buffer)) != -1) {  
  20.                 fos.write(Buffer, 0, size);  
  21.             }  
  22.         } catch (SQLException e) {  
  23.             e.printStackTrace();  
  24.         }  catch (FileNotFoundException e) {  
  25.             e.printStackTrace();  
  26.         } catch (IOException e) {  
  27.             // TODO Auto-generated catch block  
  28.             e.printStackTrace();  
  29.         }catch (Exception e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         } finally {  
  33.             try {  
  34.                 if (fos != null) {  
  35.                     fos.flush();  
  36.                     fos.close();  
  37.                 }  
  38.                 if (is != null) {  
  39.                     is.close();  
  40.                 }  
  41.             } catch (IOException e) {  
  42.                 e.printStackTrace();  
  43.             }  
  44.   
  45.         }  
  46.   
  47.     }//end else  
  48.       
  49.     return issuccess;  
  50. }  


这里读出来的图片都是.jpg格式的,因为后缀我写死了,这里还不完善。

 


注意:你需要传入已经上传到服务器的文件的地址,这个用个工具类实现BlobUtil.java。

这样管理比较清晰。

 

 

 

 

 

 

 

 

0 0