在頁面中使用圖片在應(yīng)用中是不可缺少的。很多時(shí)候我們都是把文件直接放在本地的文件系統(tǒng)上,這樣就可以直接在頁面中顯示圖片信息?,F(xiàn)在我介紹的是如何在將保存在數(shù)據(jù)庫中的圖像信息檢索出來并顯示在頁面中。所用的是oralce數(shù)據(jù)庫的blob存放圖片信息。 要顯示圖片首先在數(shù)據(jù)庫中必須要有圖片存放。第一步我們應(yīng)該建立一張表。表結(jié)構(gòu)如下:TEST_BLOB(id varchar2(20), blobcol BLOB) 第二步我們就必須將數(shù)據(jù)倒入到數(shù)據(jù)庫中 編譯運(yùn)行下面的代碼: import java.io.*; import java.sql.*; import oracle.jdbc.OracleResultSet; import oracle.sql.*;
public class TestOracle { String username,password; Connection conn = null; Statement stmt = null;
{ username="***"; password="***"; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn=DriverManager.getConnection("jdbc:oracle:thin:@10.150.6.160:1521:dbwind72",username,password); stmt=conn.createStatement(); } catch(Exception ex) { System.out.println(ex); } } public void blobInsert(String infile) throws Exception {
boolean defaultCommit = conn.getAutoCommit(); conn.setAutoCommit(false);
try { System.out.println("try start"); stmt.executeUpdate("INSERT INTO test_blob VALUES(‘111‘,EMPTY_BLOB())"); System.out.println("stmt.executeUpdate"); //conn.commit(); ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID=‘111‘ FOR UPDATE"); System.out.println("stmt.executeQuery"); while (rs.next()) { oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL"); System.out.println("rs.getBlob"); BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream()); System.out.println("out stream create"); BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(infile))); System.out.println("Create new input file"); int c; while ((c=in.read())!=-1) { out.write(c); } in.close(); out.close(); }
conn.commit(); } catch (Exception ex) { System.out.println("blobInsert‘s exception"); conn.rollback(); throw ex; }
conn.setAutoCommit(defaultCommit); }
public static void main(String[] args) { TestOracle test = new TestOracle(); try { test.blobInsert("r:\\onjava_logo.jpg"); } catch(Exception ex) { System.out.println("main‘s test.blobInsert() Exception"+ex); }
} }
這樣就把你的R盤下面的onjava_logo.jpg保存到了數(shù)據(jù)庫中。注意:BLOB型的數(shù)據(jù)必須先插入一個(gè)空值然后UPDATE,不能直接往里面插值。
好了,準(zhǔn)備工作基本完成,現(xiàn)在開始我們的主題。我在這里直接用了一個(gè)jsp文件傳送從數(shù)據(jù)庫中檢索出來的圖片信息。jsp文件代碼如下: //myimage.jsp <%@ page contentType="image/jpeg" %>
<%@ page import="java.awt.image.*,java.sql.*,com.sun.image.codec.jpeg.*,java.util.*,javax.imageio.*,java.io.*" %>
<%
// Create image String username,password,url; Connection conn = null; Statement stmt = null; BufferedInputStream inputimage=null; username="***"; password="***"; url ="jdbc:oracle:thin:@10.150.6.160:1521:dbwind72"; Class.forName("oracle.jdbc.driver.OracleDriver"); conn=DriverManager.getConnection(url,username,password); stmt=conn.createStatement();
boolean defaultCommit = conn.getAutoCommit(); conn.setAutoCommit(false);
try {
ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_BLOB WHERE ID=‘111‘");
while (rs.next()) { oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL"); inputimage = new BufferedInputStream(blob.getBinaryStream()); } } catch (Exception ex) { System.out.println("blobRead()‘s exception"+ex); conn.rollback(); throw ex; } conn.setAutoCommit(defaultCommit);
// Send back image BufferedImage image = null; try{ image = ImageIO.read(inputimage); }catch(IOException e){ System.out.println(e); } ServletOutputStream sos = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos); encoder.encode(image); inputimage.close(); /*/**/ %> 這里我用到了com.sun.image.codec.jpeg包中的JPEGImageEncoder來構(gòu)造圖片。首先從數(shù)據(jù)庫中檢索到這個(gè)BLOB保存的數(shù)據(jù)。inputimage = new BufferedInputStream(blob.getBinaryStream());然后包裝成一個(gè)BufferedInputStream。之后用這個(gè)BufferedInputStream構(gòu)造一個(gè)BufferedImage:image = ImageIO.read(inputimage);剩下的事情就好辦了,encode就行了。 簡要的說完了,希望能對(duì)大家有點(diǎn)兒幫助。
|