我想使用servlet从mysql数据库中检索图像,并在表中显示该图像以及该用户的详细信息 [英] I want to retrieve image from mysql database using servlet and show that image along with that user details in table

查看:50
本文介绍了我想使用servlet从mysql数据库中检索图像,并在表中显示该图像以及该用户的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切正常.当我删除用于获取图像的代码时,我会在表中获得用户详细信息,而当我将代码与打印用户详细信息的代码一起用于获取图像时,我只会获取图像作为输出. 我想同时显示它们和表中的内容.

Everything is working well. When I delete Code for Retrieving image then I get user details in table and when I put the code to retrieve image along with the code of printing user details then I only get image as an output. I wanted to print both of them and inside the table as other details appears.

代码如下所示:

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Welcome
 */
public class Welcome extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Welcome() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletOutputStream out =response.getOutputStream();
        response.setContentType("text/html");
        String uniroll =(String) request.getAttribute("UROLL");


        out.println("<html>");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Bootstrap/bootstrap.min.css\">");
        out.println(" <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">");
        out.println("<body>");
        out.println("<div class='container'>");
        out.println("<h3>Welcome</h3> ");
        try{

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college_record","root","20wasadk");
            String query="SELECT * FROM students_detail WHERE University_Roll=?";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, uniroll);
            ResultSet rs =ps.executeQuery();

            while(rs.next()){
                String imgLen="";
                imgLen=rs.getString(11);
                int len=imgLen.length();

                byte[] rb =new byte[len];
                InputStream readImg =rs.getBinaryStream(11);
                int index=readImg.read(rb, 0, len);


                out.println("<section class='col-md-6' id=\"signupbox\" >");
                out.println("<table class='table table-hover'>");
                out.println("<tr><th>First Name</th><td>"+ rs.getString(1)+"</td></tr>");
                out.println("<tr><th>Last Name</th><td>"+ rs.getString(2)+"</td></tr>");
                out.println("<tr><th>Sex</th><td>"+ rs.getString(3)+"</td></tr>");
                out.println("<tr><th>Father's Name</th><td>"+ rs.getString(4)+"</td></tr>");
                out.println("<tr><th>Class Roll</th><td>"+ rs.getString(5)+"</td></tr>");
                out.println("<tr><th>University Roll</th><td>"+ rs.getString(6)+"</td></tr>");
                out.println("<tr><th>Branch</th><td>"+ rs.getString(7)+"</td></tr>");
                out.println("<tr><th>Contact No</th><td>"+ rs.getString(8)+"</td></tr>");
                out.println("<tr><th>Permanent Address</th><td>"+ rs.getString(9)+"</td></tr>");
                out.println("<tr><th>Password</th><td>"+ rs.getString(10)+"</td></tr>");
                out.println("<tr><th>Index</th><td>"+index +"</td></tr>");

                ps.close();
                response.reset();
                response.getOutputStream().write(rb,0,len);
                response.getOutputStream().flush();

            }           



            out.println("<table>");
        }

        catch(ClassNotFoundException ce){
        ce.printStackTrace();
        }
        catch(SQLException se){
        se.printStackTrace();
    }   
        out.println("<section >");
        out.println("<div>");
        out.println("</body>");
        out.println("</html>");

    }

}

推荐答案

您可以对图像数据进行Base64编码,并使用img标签的src属性中的"nofollow">数据URI ,例如<img src="data:image/png;base64,iVBORw0KGgoAAAANS... (the Base64 encoded image data) ...8bgAAAAASUVORK5CYII=>.

You could Base64-encode your image data and use the data URI in the src attribute of an img tag, e.g. <img src="data:image/png;base64,iVBORw0KGgoAAAANS... (the Base64 encoded image data) ...8bgAAAAASUVORK5CYII=">.

一些注意事项:

  • 在旧版本的IE(不支持IE8且IE8中数据大小受到限制)中不支持该功能;
  • 我认为以这种方式嵌入动态图像不是一个好主意,正确的方法是拥有一个单独的servlet来仅为图像提供服务,并在src属性中使用普通的URI.请参阅上面链接页面中的重要说明部分.
  • It is not supported in old IE versions (older than IE8, and the size of data is limited in IE8);
  • I don't think it is a good idea to embed the dynamic images this way, proper way would be to have a separate servlet just to serve the images, and use a normal URI in the src attribute. See the Important Notes part in the above linked page.

这篇关于我想使用servlet从mysql数据库中检索图像,并在表中显示该图像以及该用户的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆