我想从数据库中检索图像并将其显示在jsp页面中

时间:2019-02-17 08:47:16

标签: java mysql jsp servlets

首先我有一个用于在数据库中插入图像的方法“ insertimage”,然后我使用了一个Servlet类并称为“ insertimage”方法来进行插入操作。我有一个Part类的bean类属性“ image”,并且我使用了bean类的setter方法,并设置了我从索引页获得的图像。请帮助获取代码并将其显示在jsp页面中的代码

将图像插入数据库

   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;
}

// servservt类

@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 个答案:

答案 0 :(得分:0)

假设您有一个要获取图像的jsp页面。您可以执行类似的操作以从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){}

    %>

在上面的代码中,此-> <img src="ImageProcess?id=<%=resultSet.getString("id")%>" />行很重要,这里您将parameter传递给servlet以获取特定的image

现在,在servletImageProcess中,您必须检索id中的doGet并传递查询,最后将响应发送回jsp页面。

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();

         }

这也不是完整的代码,请根据您的要求进行更改。也不要忘记添加jar's file