java读oralce中的JPG

package hz;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
* @author gongchengwei
*
* @功能
* writeimage(String id,String imagepath,String imagename)
* 从数据里去读取数据..到数据保存到文件夹中
* @参数
* 1.String id 要查录的ID
* 2.String imagepath 保存到哪里的路径.(里有一张默认图片.如果没有图片会把那一张图片用文件流写一份)
* 3.String imagename 保存的文件名.
*
* @流程
* (创建数据连接)
* 1.根据传入的路径文件名去判断文件是否存在,
*   存在.直接跳出.
* 2.通过传入的参数去查找记录.
* 3.如果记录存在.把数据库里的数据写到文件夹
* 4.不存在的话..把那一张默认文件用文件流写一张图片
*
*
*/
public class WriteImageToFile {

  private static final String URL = "jdbc:oracle:thin:@192.168.1.240:1521:persondb";
  private static final String name="personal";
  private static final String password="1234";
  private static final String driver="oracle.jdbc.driver.OracleDriver";
 
public void writeimage(String id,String imagepath,String imagename){
  /*java.util.Date  d1=new  java.util.Date(); 
  java.text.SimpleDateFormat  dformat=new  java.text.SimpleDateFormat("yyyy-MM-dd  HH:mm:ss"); 
  String  dateandtime=dformat.format(d1); 
  System.out.println("nonce time1 ="+dateandtime);
  */
   //DataBase
   Connection con = null;
   Statement stmt = null;
   ResultSet rs = null;
  
   String sql="SELECT T_IMAGE FROM T_IMAGE_LOB where T_ID = '"+id+"'";
   System.out.println(sql);
   String filepath=imagepath+imagename;  //"F:/pp/"+"1.jpg"
  
   System.out.println("filepath = "+filepath);
  
   File f=new File(filepath);
   if(f.exists()){//判断文件是否存在..如果存在直接退出
    System.out.println("文件已存在");
   }else{
    try{
    
    Class.forName(driver).newInstance();
    con = DriverManager.getConnection(URL,name,password);
    if(con!=null){
     con.setAutoCommit(true);
    }
    stmt = con.createStatement();
       rs = stmt.executeQuery(sql);

    if (rs.next()){
     Blob b = rs.getBlob("T_IMAGE");
     InputStream in=b.getBinaryStream();  // 建立输出流
     //FileInputStream in =(FileInputStream)b.getBinaryStream();
     FileOutputStream file=new FileOutputStream(filepath);
     int len=(int)b.length();
     byte[] buffer=new byte[len];  // 建立缓冲区
     System.out.println("file size ="+len);
     while((len=in.read(buffer)) != -1){
      file.write(buffer,0,len);
     }
      file.close();     
      in.close();
      con.commit();
     
    }else{
     FileInputStream infile=new FileInputStream(imagepath+"/defaultimg.jpg");
     FileOutputStream file=new FileOutputStream(filepath);
     int len=4086;
     byte[] buff = new byte[4086];
     while((len=infile.read(buff)) != -1){
      file.write(buff,0,len);
     }
      file.close();     
      infile.close();
      con.commit();
    }
   }catch(Exception e){
    //
    //System.out.println("12:"+e.getMessage());
    try {
           if(con!=null){
             con.rollback();
             con.setAutoCommit(true);
           }
       }catch (SQLException ex2){
           //throw new DatabaseException(" rollback error.", ex2);
    }
    //throw new DatabaseException("Unable to commit con, database has been rollbacked.", ex);
   }finally{
    try{
     rs.close();
     stmt.close();
     if (con != null) {
            try {
              con.close();
              con = null;
             
         }catch (SQLException ex3) {
               //throw new DatabaseException("Error occured in close con.", ex3);
            }
     }
    }catch (Exception e) {
           // throw new DatabaseException("Error occured in close con.", ex3);
          }
   }
  }
   /*
   java.util.Date  d2=new  java.util.Date(); 
   java.text.SimpleDateFormat  dformat2=new  java.text.SimpleDateFormat("yyyy-MM-dd  HH:mm:ss"); 
   String  dateandtime2=dformat.format(d2); 
   System.out.println("nonce time2 ="+dateandtime2);
   */
  }

public void delstarorg(String imagepath,String imagename){//删除过期文件
 
// System.out.println("del file = "+imagepath+imagename);
  File file =new File(imagepath+imagename);
  if(file.exists()){
   file.delete();
  }
 
}
//测试
public static void main(String args[]){
  System.out.println("start");
  WriteImageToFile witf=new WriteImageToFile();
  witf.writeimage("测试六 ,429004195508052666.jpg","F:/","cbc.jpg");
  System.out.println("end");
}


}


你可能感兴趣的:(java,apache,oracle,sql,jdbc)