簡體   English   中英

我想從數據庫中檢索圖像並將其顯示在jsp頁面中

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

首先,我有一個用於在數據庫中插入圖像的方法'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");

}
    }
}

假設您有一個要在其中獲取圖像的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您必須檢索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();

         }

這也不是完整的代碼,請根據您的要求進行更改。也不要忘記添加jar's file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM