JSP虛擬主機,jsp空間,java空間,java虛擬空間JSP虛擬主機,jsp空間,java空間,java虛擬空間

實現向MYSQL數據庫中存儲或提取圖片文件



作者:    文章來源:
發布日期:2007年03月13日
                     實現向MYSQL數據庫中存儲或提取圖片文件

 
一些情況下,需要向數據庫中存儲一些2進制文件,比如圖片文件等,這時候,向數據庫存儲數據不同于普通的字符串存儲,我們需要對這個2進制文件使用JAVA處理2進制流的API進行處理,然后再進行存儲。我們需要進行以下步驟來實現:

 
向數據庫中存儲文件的時候,一樣使用標準SQL語句,如: insert into database (column1, column2,..) values(v1,v2,…);注意的是,要在建立存放2進制文件的TABLE時,存放的字段要使用BLOB類型,而不是普通的VARCHAR等。BLOB是專門存儲2進制文件的類型,他還有大小之分,比如mediablob,logblob等,以存儲大小不同的2進制文件,一般的圖形文件使用mediablob足以了。


1 見以下代碼實現向MYSQL中儲存圖片文件:

 

…………………………

private final String insertquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";

 

public void doInsertStaffPic(String loginname,String source_URL) {

 

              Connection conn = null;

              PreparedStatement pre = null;

 

              try {

             // 進行數據庫連接,這里我使用的是在STRUTS中配置的連接池,當然也可// 以自己通過JDBC直接連

                     conn = DBProcess.getConnection();

                     

//從圖片源中獲得圖片對象并寫到緩存中

                     Image image = new ImageIcon(source_URL).getImage();

                     BufferedImage bImage = new BufferedImage(image.getWidth(null),

                                   image.getHeight(null), BufferedImage.TYPE_INT_RGB);

                     Graphics bg = bImage.getGraphics();

                     bg.drawImage(image, 0, 0, null);

                     bg.dispose();

                     

//將圖片寫入2進制的輸出流 并放如到byte[] buf中

                     ByteArrayOutputStream out = new ByteArrayOutputStream();

                     ImageIO.write(bImage, "jpg", out);

                     byte[] buf = out.toByteArray();

                     

            //獲得這個輸出流并將他設置到BLOB中

                     ByteArrayInputStream inStream = new ByteArrayInputStream(buf);

                     pre = conn.prepareStatement(insertstaffpicquery);

                     pre.setString(1, loginname);

                     pre.setBinaryStream(2, inStream, inStream.available());

                     // 執行寫如數據

pre.executeUpdate();

                     

 

              } catch (Exception exc) {

                     exc.printStackTrace();

              }

 

              finally {

                     try {

                            pre.close();

                            conn.close();

 

                     } catch (SQLException e) {

                            e.printStackTrace();

                     }

              }

 

       }

 

2 下代碼實現從MYSQL中獲取圖片文件并寫入本地文件系統:

 

 

…………………………

private final String writeoutquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";

 

// retrive the picture data from database and write it to the local disk

       public void doGetAndShowStaffPic(String loginname, String dir) {

 

        FileOutputStream output = null;

        InputStream input = null;

 

              Connection conn = null;

              ResultSet rs = null;

              PreparedStatement pre = null;

 

              try {

                     conn = DBProcess.getConnection();

                     pre = conn.prepareStatement(writeoutquery);

                     pre.setString(1, loginname);

                     rs = pre.executeQuery();

 

                     if (rs.next()) {

                // 從數據庫獲得2進制文件數據

                            Blob image = rs.getBlob("Binary_Photo");

                            // setup the streams

                            Input = image.getBinaryStream();
                   

                            try {

                    // 設置寫出路徑。

                                   output = new FileOutputStream(dir);

                            } catch (FileNotFoundException e1) {

                                   

                                   e1.printStackTrace();

                            }

                            // set read buffer size 注意不要設置的太小,要是太小,圖片可能不完整

                            byte[] rb = new byte[1024000];

                            int ch = 0;

                            // process blob

 

                            try {

                   // 寫入本地文件系統

                                   while ((ch = input.read(rb)) != -1) {

                                          output.write(rb, 0, ch);

                                          

                                   }

                                   

                            } catch (IOException e) {

                                   

                                   e.printStackTrace();

                            }

                            

                            try {

                                   input.close();

                            } catch (IOException e) {

                                   

                                   e.printStackTrace();

                            }

                            try {

                                   output.close();

                            } catch (IOException e) {

                            

                                   e.printStackTrace();

                            }

 

                     }

 

              } catch (SQLException e) {

                     e.printStackTrace();

              }

 

              finally {

                     try {

                            rs.close();

                            pre.close();

                            conn.close();

 

                     } catch (SQLException e) {

                            e.printStackTrace();

                     }

              }

       }

 
Copyright © 2002-2012 JSPCN.net. All rights reserved.
JSP中文網    備案號:粵ICP備09171188號
成都恒海科技發展有限公司    成都市一環路南二段6號新瑞樓三樓8號
一肖中特公开威up