2013-02-07 48 views
0

我試過這段代碼來顯示圖像從MySQL數據庫使用blob ...圖像不可見。 我已經使用這段代碼。jsp圖像顯示

請糾正這個錯誤

<head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <img src="WEB-INF/New folder/birthhappy birthday.jpeg" 
     width="65" height="71" alt="birthhappy birthday"/>  
     <H1>Database Lookup</H1> 
     <FORM ACTION="base.jsp" METHOD="POST"> 
      Please enter the ID of the publisher you want to find: 
      <BR> 
      <input type="text" name="imagename"> 
      <BR> 
      <INPUT TYPE="SUBMIT" value="Submit"> 
     </FORM> 


    </body> 
</html> 

base.jsp:

<%@page import="java.io.OutputStream"%> 
<%@ page import="java.sql.*" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 

     <% 

     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/searchimg","root",""); 
     Statement stmt = con.createStatement(); 
      //String id = request.getParameter("id"); 
      String imagename=request.getParameter("imagename"); 

      ResultSet resultset =stmt.executeQuery("select * from friendupload where imagename = '" +imagename.trim()+ "' ") ; 

while(resultset.next()) 
{ 




%>          

<table> 
    <tr> 
     <td> <%=resultset.getString(1)%>   </td> 
     <td> <%=resultset.getString(2) %> </td> 
     <td> <%=resultset.getString(3)%> </td> 
       <td>  
        Blob getimg=rs.getBlob(4); 
     InputStream readImg = getimg.getBinaryStream(); 
     int size=readImg.available(); 
     OutputStream outf=new FileOutputStream("D:/profile/"+rs.getString(2)+rs.getString(3)+".jpeg"); 

     byte b[]= new byte[size]; 
      readImg.read(b); 
      outf.write(b); 
      outf.close(); 



     </td> 

    </tr> 
    </table> 

<% }%> 

     </form> 

    </body> 
</html> 

回答

0

給您斑後存儲,因此圖像正確的路徑保存圖像,你應該使用圖像加載它標記,只是給blob不會顯示圖像。

<%while(resultset.next()) 
{ 
%>          
<table> 
<tr> 
    <td> <%=resultset.getString(1)%>   </td> 
    <td> <%=resultset.getString(2) %> </td> 
    <td> <%=resultset.getString(3)%> </td> 
      <td>  
       Blob getimg=rs.getBlob(4); 
    InputStream readImg = getimg.getBinaryStream(); 
    int size=readImg.available(); 
    OutputStream outf=new FileOutputStream("D:/profile/"+rs.getString(2)+rs.getString(3)+".jpeg"); 

    byte b[]= new byte[size]; 
     readImg.read(b); 
     outf.write(b); 
     outf.close(); 

    <image src="<%=path%>" /> 

    </td> 

</tr> 
</table> 

你檢查過的圖像文件是越來越保存在您指定日的位置。

0

<form>標籤丟失,也確保d:\資料/目錄中 使用<img>元素表單元素

0

有時瀏覽器不允許你使用的資源,從本地硬盤。

您最好編寫一個ImageServlet來直接傳遞這些圖像,而不必將它們寫入文件。

注意:如果您使用chunked-stream(默認)傳送這些圖像,請指定內容長度!否則,瀏覽器將不知道響應何時結束。這會導致防病毒程序永無止境地分析這些圖像。

BTW:WEB-INF /新建文件夾/ birthhappy birthday.jpeg不會被訪問,嘗試使用WEB-INF /班/ META-INF /資源/新建文件夾/ birthhappy birthday.jpeg