在java中使用其他正常数据值从servlet检索图像到jsp

时间:2014-08-05 09:30:50

标签: java image jsp servlets

我想从数据库中检索存储为blob的图像,这个图像是员工的个人资料图像,因此图像中的其他一些细节会像名称,名称一样,所以我在employeedetail中传递servlet路径和值。 jsp,它是去那个servlet,一切都是正确的,如查询,参数,但最后在employeedetail.jsp页面图像不来它只是显示一个十字标志那里请任何人帮我这里是代码..

Login.Java

public class Login extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        HttpSession session = request.getSession();
        // InputStream sImage;
        String sid = request.getParameter("eid");
        session.setAttribute("sid", sid);
        String password = request.getParameter("password");

        try {
            Connection con = ConnectionManager.getConnection();

            String query = "select eid, name, password, sex, dob, bloodgroup, fathername, qualification, mailid, contactnum, skills, temporaryadd, permanentadd, access_type
            from empinfo where eid = '" + sid+ "' AND password = '" + password + "' ";

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            if (rs.next()) {
                String n1 = rs.getString("eid");
                String n2 = rs.getString("name");
                String n3 = rs.getString("password");
                String n4 = rs.getString("sex");
                String n5 = rs.getString("dob");
                String n6 = rs.getString("bloodgroup");
                String n7 = rs.getString("fathername");
                String n8 = rs.getString("qualification");
                String n9 = rs.getString("mailid");
                String n10 = rs.getString("contactnum");
                String n11 = rs.getString("skills");
                String n12 = rs.getString("temporaryadd");
                String n13 = rs.getString("permanentadd");
                String n14 = rs.getString("access_type");
                //  sImage = rs.getBinaryStream("image");
                session.setAttribute("eid", n1);
                session.setAttribute("name", n2);
                session.setAttribute("password", n3);
                session.setAttribute("sex", n4);
                session.setAttribute("dob", n5);
                session.setAttribute("bloodgroup", n6);
                session.setAttribute("fathername", n7);
                session.setAttribute("qualification", n8);
                session.setAttribute("mailid", n9);
                session.setAttribute("contactnum", n10);
                session.setAttribute("skills", n11);
                session.setAttribute("temporaryadd", n12);
                session.setAttribute("permanentadd", n13);
                session.setAttribute("access_type", n14);
                //session.setAttribute("image",sImage);
                response.sendRedirect("EmployeeDetail.jsp");
            } else {

                String message = "Your are not a registered employee.";
                request.setAttribute("message", message);
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
        } catch (Throwable theException) {
            System.out.println(theException);
        }
    }
}

Employeedetail.jsp

<html>
<head>
  <style>
  </style>
</head>
<body>
<table align=center cellspacing=1 cellpadding=3>
  <tr><Td> Employee name</td><Td><%=name%></td></tr>
  <tr>
  <td><img src="${pageContext.servletContext.contextPath }/retImage?param1=  <%=eid%>">image</td>
    // here i am passing the value and it is passing to servlet also
  </tr>
  <tr><Td>Password</td><Td><%=password%></td></tr>
  <tr><Td>Sex</td><Td><%=sex%></td></tr>
  <tr><Td>dob</td><Td><%=dob%></td></tr>
  <tr><Td>Blodgroup</td><Td><%=bloodgroup%></td></tr>
  <tr><Td>fathername</td><Td><%=fathername%></td></tr>
  <tr><Td>Qualification</td><Td><%=qualification%></td></tr>
  <tr><Td>mail id</td><Td><%=mailid%></td></tr>
  <tr><Td>contact num</td><Td><%=contactnum%></td></tr>
  <tr><Td>skills</td><Td><%=skills%></td></tr>
  <tr><Td>local address</td><Td><%=temporaryadd%></td></tr>
  <tr><Td>permanent Address</td><Td><%=permanentadd%></td></tr>
  <tr><Td>Emp Category</td><Td><%=access_type%></td></tr>
</table><BR>
</div>
</body>
</html>

retImage.java

public class retImage extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException
    {
        HttpSession session = request.getSession(true);

        String sid=(String)session.getAttribute("eid");

        session.setAttribute("sid",sid);
        System.out.println("sid is " +sid);    

        String iid = request.getParameter("param1");
        InputStream sImage;
        try {

            Connection con = ConnectionManager.getConnection();
            String Query= "SELECT image FROM empinfo WHERE eid ='"+iid+"'";
            System.out.println("Query is" +Query);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(Query);
            System.out.println("..........1");
            if(rs.next())
            {
                System.out.println("........2");

                byte[] bytearray = new byte[1048576];
                int size=0;

                sImage = rs.getBinaryStream(1);    
                response.reset();

                while((size=sImage.read(bytearray))!= -1 ) {
                    response.getOutputStream().write(bytearray,0,size);
                    System.out.println(".......3");
                }

            }
        }
        catch(Exception ex){
            System.out.println("error :"+ex);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在retImage.java中添加finally块,如下所示: -

public class retImage extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException
    {
       ......
       // replace InputStream sImage with sImageBytes
       byte[] sImageBytes;
        try {           
            .....
            if(rs.next())
            {
                ...
                sImageBytes = rs.getBytes()

                response.setContentType("image/jpeg");
                response.setContentLength(sImageBytes.length);
                // Give the name of the image in the name variable in the below line   
                response.setHeader("Content-Disposition", "inline; filename=\"" + name+ "\"");

                BufferedInputStream  input = new BufferedInputStream(new ByteArrayInputStream(sImageBytes));
                BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());

                byte[] buffer = new byte[8192];
                int length;
                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                    System.out.println(".......3");
                }
            }
        }
        catch(Exception ex){
            System.out.println("error :"+ex);
        }
    }
}