2013-08-19 97 views
0

我在JSP中創建了一個程序來獲取圖像並將其顯示在網頁上。程序正常工作圖像顯示但其他內容不顯示。下面是代碼顯示數據庫中的圖像

<% 
     byte[] imgData = null ; 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/buysell","root","root"); 
     Statement stmt = con.createStatement();  
ResultSet resultset =stmt.executeQuery("select * from imagemain where id=1;") ; 
    while(resultset.next()) 
    { 
     Blob bl = resultset.getBlob(2); 
byte[] pict = bl.getBytes(1,(int)bl.length()); 
response.setContentType("image/jpg"); 
OutputStream o = response.getOutputStream(); 
%> 
<img src="<%o.write(pict);%>" width="10" height="10"> 
<h1>Vishal</h1> 
<% 
out.print("1"); 
o.flush(); 
o.close(); 
    } 
     %> 

程序不顯示<h1>Vishal</h1>。請幫助這個

+0

調試?查詢是否工作 - >在數據庫客戶端(MySQL管理員,SQLYOG或phpmyadmin)中運行它以查看。 while循環運行?結果集可能是NULL? –

+0

感謝Raymond爲您的迴應...圖像顯示正確,但其他HTML內容像「

Vishal

」無法正確顯示。 – vishal

+0

您正在返回一張圖片。要顯示文本和圖像,您需要兩次http訪問或使用B64編碼創建數據URI。在這兩種情況下,你需要開始與文本/ html – mplungjan

回答

1

您需要在http如何標準訪問讀了作品

現在嘗試

response.setContentType("text/html"); 
OutputStream o = response.getOutputStream(); 
%><img src="data:image/jpg;base64, <%o.write(Base64.encode(pict));%>" width="10" height="10"> 
    <h1>Vishal</h1> 

此處瞭解詳情:How to display an image which is in bytes to JSP page using HTML tags?

OR

<img src="otherjspreturningimage.jsp" /> 
+0

我收到一個錯誤「Base64找不到符號」 – vishal

+0

您需要爲您的項目添加一個b64編碼器。試試谷歌! https://www.google.com/search?q=jsp+b64+encoder – mplungjan