2013-10-17 194 views
0

我想從mysql數據庫中使用java檢索幾個blob圖像。現在,問題是我一次只能得到一張圖像,其他圖像沒有得到顯示。如何使用jsp從mysql中檢索多個blob圖像

下面是我的JSP代碼,在那裏我這樣做(只是爲了演示目的):

<?xml version="1.0" encoding="UTF-8" ?> 
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 
    <%@ page language="java" %> 
    <%@ page import="java.sql.*" %> 
    <%@ page import="java.io.*" %> 
    <%@ page import="java.util.*"%> 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
    <title>MindDotEditor posted Data</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <meta name="robots" content="noindex, nofollow" /> 
    <link href="../sample.css" rel="stylesheet" type="text/css" /> 
    <link rel="shortcut icon" href="../fckeditor.gif" type="image/x-icon" /> 
    </head> 
    <body> 

     <% 
String url = "jdbc:mysql://localhost:3306/grandsho_register"; 
Connection con = null; 
Statement stmt = null; 
ResultSet rs = null; 


try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    con = DriverManager.getConnection(url,"root","root"); 
    stmt = con.createStatement(); 
    rs = stmt.executeQuery("SELECT image FROM user "); 
    int i = 1; 
    if(rs.next()) { 
     Blob len1 = rs.getBlob("image"); 
     int len = (int)len1.length(); 
     byte[] b = new byte[len]; 
     InputStream readImg = rs.getBinaryStream(1); 
     int index = readImg.read(b, 0, len); 
     System.out.println("index" +index); 
     stmt.close(); 
     response.reset(); 
     response.setContentType("image/jpg"); 
     response.getOutputStream().write(b,0,len); 
     response.getOutputStream().flush(); 
    } 
} catch(Exception ex) { 
    out.println(ex); 
} finally { 
    rs.close(); 
    stmt.close(); 
    con.close(); 
} 
     %> 

    <br> 
    <center><input type="button" value="Print" onclick="window.print();return false;" />  </center> 
    </body> 
    </html>  

任何人都可以建議我怎麼能顯示JSP頁面上多張圖片?

+0

@ShyreyosAdikari你能告訴我我需要改變的地方嗎,特別是當我得到結果集時?早些時候已經通過你的鏈接。 – puneetjava

回答

0

而不是if(rs.next()) {},你必須使用while(rs.next()){}

-1

嗨,大家好爲你所有的問題我將要給你的解決方案:從仇恨編碼要知道,通過檢索多個圖像

1.首先件事像(從照片中選擇圖片)查詢是不可能的,你可以一次只檢索一張圖片,但我們可以通過一些不同的方式檢索多張圖片。

[* - >更重要的是,我希望你們已經將圖像上傳到分貝這裏我只顯示檢索多個圖像,如果你想圖像上傳的代碼,然後把你的郵件編號在這裏]

2.對於圖像操作首先你需要將這些jar文件添加到您的項目Libaries

--->公地文件上傳-1.3.jar http://www.java2s.com/Code/Jar/c/Downloadcommonsfileupload13jar.htm

--->的commons-IO-2.4 .jar http://www.java2s.com/Code/Jar/c/Downloadcommonsio24jar.htm

3.創建使用SQL查詢(我使用MySQL)的表

創建表upload_image(ID INT NOT NULL AUTO_INCREMENT主鍵,bImage BLOB);

4.一旦進口的罐子和創建表的時間做一些不好的事情,多數民衆贊成編碼,添加代碼

第一的html代碼: < ----- -----的index.jsp - >

`

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Reterving multiple images</title> 
</head> 

<body> 
    <img src="veiw.jsp?id=1"> 
    <img src="veiw.jsp?id=2"> 
</body> 

</html> 

`

< ----- veiw.jsp ------>

[<%@ page import="java.sql.*" %> 
<%@ page import="org.apache.commons.fileupload.*"%> 
<%@ page import="org.apache.commons.io.output.*"%> 
<%@ page import="org.apache.commons.fileupload.servlet.*"%> 
<%@ page import="org.apache.commons.fileupload.disk.*"%> 
<%@ page import="java.io.*"%> 
<%@ page import="java.util.*"%> 
<% 
String url="jdbc:mysql://localhost:3306/testzeroDateTimeBehavior=convertToNull"; 
Connection con = null; 
Statement stmt = null; 
ResultSet rs = null; 
String j=request.getParameter("id"); 
int i=Integer.parseInt(j); 
try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    con = DriverManager.getConnection(url,"root",""); 
    stmt = con.createStatement(); 
    rs = stmt.executeQuery("SELECT bImage FROM upload_image where id="+i); 
     OutputStream o = response.getOutputStream(); 
    if(rs.next()) 
    { 
      Blob bl = rs.getBlob(1); 
      byte\[\] pict = bl.getBytes(1,(int)bl.length()); 
      response.setContentType("image/jpg"); 
      o.write(pict); 
      o.flush(); 
      o.close(); 


    } 

} 
catch(Exception ex) 
{ 
    out.println(ex); 
} 
finally 
{ 
    rs.close(); 
    stmt.close(); 
    con.close(); 
} 

%>[image shows the added jar file then start processing from step 3 ][3] 
+0

在這裏我已經給出了2個圖像的演示已經存在於分貝 – viswarajramji

相關問題