0

First i have a method 'insertimage' for inserting the image in the database and then I have used a Servlet class and called the method 'insertimage' for inserting operation.I have a bean class property 'image' of Part type and I have used a setter method of the bean class and set the image which i got from the index page. please help with the code for retrieving the image and displaying it in a jsp page

inserting the image the database

   public boolean insertimage(FoodItems food)
{
boolean result=false;
try
{
    InputStream inputstream=null;
image=food.getImage();// i have a bean class property of Part type 
    if(image !=null)
    {
        long fileSize=image.getSize();
        String  fileContent=image.getContentType();
        inputstream=image.getInputStream();
    }
    PreparedStatement pst=con.prepareStatement("insert into AvailableItems values(?)");
    pst.setBlob(1,inputstream);
    pst.executeUpdate();
    result=true;
}
catch(Exception e)
{
    System.out.println("error st Available insert"+e);
}
return result;
}

//servlert class

@MultipartConfig(maxFileSize=169999999)

@WebServlet("/InsertFoods") 
    public class InsertFoods extends HttpServlet


 {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 

{
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Part image=request.getPart("image");
DBOperations db=new DBOperations();
FoodItems food=new FoodItems();
food.setImage(image);
if(db.insertimage(food))
{
    response.sendRedirect("AvailableItems.jsp");
}
else
{
    pw.println("not inserted");

}
    }
}

1 Answers1

0

Suppose you have jsp page where you want to retreive image . you can do something like this to retrieve any image from database.

 <%   //dbconnection
          try {
                   Class.forName("com.mysql.jdbc.Driver");
                 java.sql.Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");
                  Statement statement = conn.createStatement() ;
       resultSet=statement.executeQuery("select * from tablename") ; 
                %>
       <% while(resultSet.next()){ %> 

   Image - <img src="ImageProcess?id=<%=resultSet.getString("id")%>" width="20%"/>

    <% 
    }
    }catch(Exception e){}

    %>

In Above code this -> <img src="ImageProcess?id=<%=resultSet.getString("id")%>" />line is important ,here you are passing parameter to servlet to get particular image

Now,in your servleti.e ImageProcess you have to retreive the id in doGet and pass in query ,lastly send back the reponse to jsp page .

Blob image = null;
        byte[] imgData = null;
       String id= request.getParameter("id");//here you are getting id 
       int i;
       ResultSet rs =null;

 try {

            //loading drivers for mysql
           Class.forName("com.mysql.jdbc.Driver");
             Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");


         String sql = "SELECT image FROM tablename where id=?"; //here pass that id in query to get particular image 

           PreparedStatement ps = con.prepareStatement(sql);


               ps.setString(1, id);
              rs = ps.executeQuery();    
 while (rs.next()) {
                  image = rs.getBlob("image");//getting image from database 
                  imgData = image.getBytes(1,(int)image.length()); //extra info about image
                } 

response.setContentType("image/gif");//setting response type



OutputStream o = response.getOutputStream();

o.write(imgData);//sending the image to jsp page 
o.flush();
o.close();


 }
    catch(Exception e)
         {
             e.printStackTrace();

         }

Also this is not complete code,make changes as per your requirements .also don't forget to add jar's file

Swati
  • 28,069
  • 4
  • 21
  • 41