0

I have tried this code to display image from MySQL database using blob...image is not visible. I have used this code.

Please rectify this error

<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>
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MohanRaj S
  • 1,958
  • 4
  • 30
  • 54

3 Answers3

0

Give proper path to the image that you are storing so after the blob is saving the image you should load it using image tag, just giving blob wont show up the image.

<%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>

Have you checked the image file is getting saved in th location that you have specified.

Meherzad
  • 8,433
  • 1
  • 30
  • 40
0

your <form> tag is missing and also make sure that d:\profile/ directory exist use <img> element in form element

Manohar Bomma
  • 311
  • 1
  • 3
  • 17
0

Sometimes the browser does not allow you to use resources from local harddrive.

You better write an ImageServlet to deliver those images directly without write them into a file.

Note: If you deliver those images using chunked-stream(default) specify the content-length! Otherwise the browser will not know when the response has been ended. This causes antivirus-programs to never-ending-analyse those images.

BTW: WEB-INF/New folder/birthhappy birthday.jpeg wont be accessible, try to use WEB-INF/classes/META-INF/resources/New folder/birthhappy birthday.jpeg

Grim
  • 1,938
  • 10
  • 56
  • 123