How to retrieve and display images from a database in a JSP page?

前端 未结 5 1890
渐次进展
渐次进展 2020-11-21 04:37

How can I retrieve and display images from a database in a JSP page?

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 04:52

    I used SQL SERVER database and so the answer's code is in accordance. All you have to do is include an tag in your jsp page and call a servlet from its src attribute like this

    
    

    Here 1 is unique id of image in database and ID is a variable. We receive value of this variable in servlet. In servlet code we take the binary stream input from correct column in table. That is your image is stored in which column. In my code I used third column because my images are stored as binary data in third column. After retrieving input stream data from table we read its content in an output stream so it can be written on screen. Here is it

    import java.io.*;  
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.servlet.*;  
    import javax.servlet.http.*;  
    import model.ConnectionManager;
    public class DisplayImage extends HttpServlet { 
        public void doGet(HttpServletRequest request,HttpServletResponse response)  
                 throws IOException  
        { 
        Statement stmt=null;
        String sql=null;
        BufferedInputStream bin=null;
        BufferedOutputStream bout=null;
        InputStream in =null;
    
        response.setContentType("image/jpeg");  
        ServletOutputStream out;  
        out = response.getOutputStream();  
        Connection conn = ConnectionManager.getConnection();
    
        int ID = Integer.parseInt(request.getParameter("ID"));
            try {
                stmt = conn.createStatement();
                sql = "SELECT * FROM IMAGETABLE WHERE ID="+ID+"";
                ResultSet result = stmt.executeQuery(sql);
                if(result.next()){
                    in=result.getBinaryStream(3);//Since my data was in third column of table.
                }
                bin = new BufferedInputStream(in);  
                bout = new BufferedOutputStream(out);  
                int ch=0;   
                while((ch=bin.read())!=-1)  
                    {  
                    bout.write(ch);  
                }  
    
            } catch (SQLException ex) {
                Logger.getLogger(DisplayImage.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
            try{
                if(bin!=null)bin.close();  
                if(in!=null)in.close();  
                if(bout!=null)bout.close();  
                if(out!=null)out.close();
                if(conn!=null)conn.close();
            }catch(IOException | SQLException ex){
                System.out.println("Error : "+ex.getMessage());
            }
        }
    
    
        }  
    }  
    

    After the execution of your jsp or html file you will see the image on screen.

提交回复
热议问题