天天看點

向oracle中插入圖檔和讀取圖檔向oracle中插入圖檔和讀取圖檔

向oracle中插入圖檔和讀取圖檔

代碼如下:

package demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
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 com.mysql.fabric.xmlrpc.base.Data;

public class Test {
	 private Connection conn;
	 
	    /**
	     * 得到一個資料庫的連接配接
	     * 
	     * @return 返加Connection對象
	     */
	    public Connection getConnection() {
	        try {
	            Class.forName("oracle.jdbc.driver.OracleDriver");
	            conn = DriverManager.getConnection(
	                    "jdbc:oracle:thin:@localhost:1521:mldn", "scott", "tiger");
	        } catch (ClassNotFoundException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        } catch (SQLException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        return conn;
	    }
	 
	    /**
	     * 向表中插入圖檔
	     * 
	     * @param path圖檔所在的路徑
	     * @return 整形 判斷成功或失敗
	     */
	    public int insertImage(String path) throws Exception {
	    	System.out.println("----insertPhoto");
	        Connection conn = null;
	    	int i = 0;
	        Statement st = null;
	        ResultSet rs = null;
	        conn=this.getConnection();
	         
	        conn.setAutoCommit(false);//設定資料庫為不自動送出,必須的一步
	        st = conn.createStatement();//擷取一個可以執行sql語句的對象
	        
	        i = st
	                .executeUpdate("insert into IMAGE1 (\"id\",\"image\") values (2,'1')");
	        
	        rs = st
	                .executeQuery("select \"image\" from IMAGE1 where \"id\"=(select max(\"id\") from IMAGE1) for update");
	        if (rs.next()) {
	            //得到流
	            oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
	            //從得到的低級流構造一個進階流
	            PrintStream ps = new PrintStream(blob.getBinaryOutputStream());
	            BufferedInputStream bis = new BufferedInputStream(
	                    new FileInputStream(path));
	            byte[] buff = new byte[1024];
	            int n = 0;
	            //從輸入到輸出
	            while ((n = bis.read(buff)) != -1) {
	                ps.write(buff, 0, n);
	 
	            }
	            //清空流的緩存
	            ps.flush();
	            //關閉流,注意一定要關
	            ps.close();
	            bis.close();
	        }
	        rs.close();
	        st.close();
	        conn.close();
	        return i;
	    }
	
	    /**
	     * 從oracle中讀取圖檔
	     * 
	     * @param path圖檔所在的路徑
	     * @return 整形 判斷成功或失敗
	     */
	    public int getImage(String path) throws Exception {
	    	System.out.println("----getPhoto");
	    	int j = 0;
	        Statement  st = null;
	        ResultSet rs = null;
	        conn=this.getConnection();
	        conn.setAutoCommit(false);//設定資料庫為不自動送出,必須的一步
	        st = conn.createStatement();//擷取一個可以執行sql語句的對象
	        rs =  st
	                .executeQuery("select \"image\" from IMAGE1 where \"id\"=(select max(\"id\") from IMAGE1) ");
	       if (rs.next()) {
	    	   java.sql.Blob blob = rs.getBlob(1);
	    	    InputStream ins = blob.getBinaryStream();
	    	     //用檔案模拟輸出流
	    	File file = new File("d:\\output.txt");
	    	    OutputStream fout = new FileOutputStream(file);
	    	     //下面将BLOB資料寫入檔案
	    	     byte[] b = new byte[1024];
	    	     int len = 0;
	    	         while ( (len = ins.read(b)) != -1) {
	    	           fout.write(b, 0, len);
		} 
	    	         //依次關閉
	    	         j=2;
	    	         fout.close();
	    	         ins.close();
	    	         conn.commit();
	    	         conn.close();                
	        }
	        rs.close();
	        st.close();
	        return j;
	    }

	    public static void main(String[] args) throws Exception {
	    	int out;
	    	int out2;
	        Test test=new Test();
	       out= test.insertImage("e://psb.jpg");
	       System.out.println(out);
	       System.out.println("上傳圖檔成功!");
	       out2=test.getImage("e://psb.jpg");
	       System.out.println(out2);
	       System.out.println("讀取圖檔成功!");
	 
	 
	    }
	 
}
           

執行後如下圖所示: 

向oracle中插入圖檔和讀取圖檔向oracle中插入圖檔和讀取圖檔
向oracle中插入圖檔和讀取圖檔向oracle中插入圖檔和讀取圖檔

用畫闆打開下載下傳的output.txt,就可以看到你下載下傳的圖檔了 

向oracle中插入圖檔和讀取圖檔向oracle中插入圖檔和讀取圖檔