2012-09-06 153 views
0

我想通過此Servlet使用HTML表單<input type="file" name="file" />將圖像插入到MySQL中,因此我使用此代碼。通過html表格將圖像插入到數據庫中

if (!isMultipart) { 
    System.out.println("File Not Uploaded"); 
} else { 
    FileItemFactory factory = new DiskFileItemFactory(); 
    ServletFileUpload upload = new ServletFileUpload(factory); 
    List items = null; 

    try { 
     items = upload.parseRequest(request); 
     System.out.println("items: "+items); 
    } catch (FileUploadException e) { 
     e.printStackTrace(); 
    } 

    Iterator itr = items.iterator(); 

    while (itr.hasNext()) { 
     FileItem item = (FileItem) itr.next(); 

     if (item.isFormField()){ 
      String name = item.getFieldName(); 
      System.out.println("name: "+name); 
      String value = item.getString(); 
      System.out.println("value: "+value); 
     } else { 
      try { 
       String itemName = item.getName(); 
       Random generator = new Random(); 
       int r = Math.abs(generator.nextInt()); 

       String reg = "[.*]"; 
       String replacingtext = ""; 
       System.out.println("Text before replacing is:-" + itemName); 
       Pattern pattern = Pattern.compile(reg); 
       Matcher matcher = pattern.matcher(itemName); 
       StringBuffer buffer = new StringBuffer(); 

       while (matcher.find()) { 
        matcher.appendReplacement(buffer, replacingtext); 
       } 

       int IndexOf = itemName.indexOf("."); 
       String domainName = itemName.substring(IndexOf); 
       System.out.println("domainName: "+domainName); 

       String finalimage = buffer.toString()+"_" + r + domainName; 
       System.out.println("Final Image===" + finalimage); 

       File savedFile = new File("/home/abdo/NetBeansProjects/Gestion_des_taches/web/"+"img/"+finalimage); 
       item.write(savedFile); 
       out.println("<html>"); 
       out.println("<body>"); 
       out.println("<table><tr><td>"); 
       out.println("<img src=images/" + finalimage + ">"); 
       out.println("</td></tr></table>"); 

       Connection conn = null; 
       String url = "jdbc:mysql://localhost/"; 
       String dbName = "actors"; 
       String driver = "com.mysql.jdbc.Driver"; 
       String username = "root"; 
       String userPassword = "root"; 
       String strQuery = null; 
       String strQuery1 = null; 
       String imgLen=""; 

       try { 
        System.out.println("itemName::::: "+itemName); 
        Class.forName(driver).newInstance(); 
        conn = DriverManager.getConnection(url+dbName,username,userPassword); 
        Statement st = conn.createStatement(); 
        strQuery = "insert into pictures set image='" + finalimage + "'"; 
        int rs = st.executeUpdate(strQuery); 
        System.out.println("Query Executed Successfully++++++++++++++"); 
        out.println("image inserted successfully"); 
        out.println("</body>"); 
        out.println("</html>");      
       } catch (Exception e) { 
        System.out.println(e.getMessage()); 
       } finally { 
        conn.close(); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

當我使用

out.println("<img src=images/" + finalimage + ">"); 

它工作正常。我的問題是如何通過MySQL請求select image from pictures;顯示圖像?

這裏是pictures表的輸出:

mysql> select * from pictures; 

+----+----------------------+ 
| id | image    | 
+----+----------------------+ 
| 42 | Photo_1339972050.jpg | 
| 43 | Photo_168423959.jpg | 
| 44 | Photo_859939969.jpg | 
| 45 | Photo_1696305644.jpg | 
| 46 | Photo_910632756.jpg | 
| 47 | Photo_560808853.jpg | 
+----+----------------------+ 
6 rows in set (0.00 sec) 
+0

是否要將圖像存儲在數據庫表中?或者你只是將圖像的名稱或路徑存儲在數據庫表和圖像本身文件系統上? –

+0

此代碼將圖像存儲在File savedFile = new File(「/ home/abdo/NetBeansProjects/Gestion_des_taches/web /」+「img /」+ finalimage); ,但在表格中,我只找到了名字,當我給他們看時,我只知道名字,有任何想法可以從該文件夾中顯示出來? – abdo

回答

0

您將需要一個額外的servlet用於下載的實際圖像數據。檢查BalusC的FileServlet