简体   繁体   English

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

[英]I want to retrieve image from the database and display it in a jsp page

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. 首先,我有一个用于在数据库中插入图像的方法'insertimage',然后我使用了一个Servlet类并称为方法'insertimage'进行插入操作。我有一个Part类的bean类属性'image',并且我已经使用了bean类的setter方法,并设置我从索引页获得的图像。 please help with the code for retrieving the image and displaying it in a jsp page 请帮助获取代码并将其显示在jsp页面中的代码

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 // 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");

}
    }
}

Suppose you have jsp page where you want to retreive image . 假设您有一个要在其中获取图像的jsp页面。 you can do something like this to retrieve any image from database . 您可以执行类似的操作以从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 在上面的代码中,此-> <img src="ImageProcess?id=<%=resultSet.getString("id")%>" />行很重要,这里您将parameter传递给servlet以获取特定的image

Now,in your servlet ie ImageProcess you have to retreive the id in doGet and pass in query ,lastly send back the reponse to jsp page . 现在,在您的servletImageProcess您必须检索doGetid并传递查询,最后将响应发送回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();

         }

Also this is not complete code,make changes as per your requirements .also don't forget to add jar's file 这也不是完整的代码,请根据您的要求进行更改。也不要忘记添加jar's file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数据库检索图像并显示在另一个jsp页面中 - Retrieve Image from Database and display in another jsp page 我想在jsp中每页显示数据库10的结果 - I want to display results from database 10 per page in jsp 我想从数据库中检索图像并在JSP中使用显示它 <img> 标签。 我正在使用我的SQL - I want to retrieve image from database and display it in JSP using <img> tag. I am using my sql 如何显示从数据库到JSP页面的图像 - How to display a a image from database to JSP page 如何在 JSP 页面中从数据库中检索和显示图像? - How to retrieve and display images from a database in a JSP page? 如何从Oracle数据库检索图像并在Struts中的JSP中显示 - How to retrieve image from an Oracle database and display in JSP in Struts 如何使用Jsp页面在数据库中上载图像以及如何从数据库中检索图像并在页面中显示另一个图像 - How to upload image in database using Jsp page And how to retrieve image from Database and show another in page 如何在jsp页面上显示mysql数据库中的图像和文本 - how to display image and text from mysql database on jsp page 如何从jsp页面上的数据库动态显示图像 - How to display an image dynamically from database on the jsp page 如何在jsp页面中从数据库mysql加载和显示图像? - How to load and display a image from a database mysql in a jsp page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM