Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用servlet在JSP中显示数据库中的图像_Java_Mysql_Jsp_Servlets - Fatal编程技术网

Java 无法使用servlet在JSP中显示数据库中的图像

Java 无法使用servlet在JSP中显示数据库中的图像,java,mysql,jsp,servlets,Java,Mysql,Jsp,Servlets,我正在尝试使用servlet从mysql数据库中获取并显示保存为JSP上blob的图像。我写的这段代码引用了很多网站,但仍然不起作用。我没有收到任何类型的错误。它是这样显示的 我已经使用 create table contacts(id int not null auto_increment, name varchar(40), second varchar(40), photo blob, primary key(id)); 这是我的Servlet DisplayServlet.java

我正在尝试使用servlet从mysql数据库中获取并显示保存为JSP上blob的图像。我写的这段代码引用了很多网站,但仍然不起作用。我没有收到任何类型的错误。它是这样显示的

我已经使用

create table contacts(id int not null auto_increment,
name varchar(40),
second varchar(40),
photo blob,
primary key(id));
这是我的Servlet DisplayServlet.java

@WebServlet("/DisplayServlet")
public class DisplayServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    public void init() throws ServletException {

    }

public DisplayServlet() {
    super();
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String imageId = request.getParameter("id");
    System.out.println(imageId);
    InputStream sImage;



    // Check if ID is supplied to the request.
    if (imageId == null) {
        // Do your thing if the ID is not supplied to the request.
        // Throw an exception, or send 404, or show default/warning image, or just ignore it.
        response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
        return;
    }

    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectbuy", "root","root");
        stmt = conn.prepareStatement("select photo from contacts where id=" + imageId);
        rs = stmt.executeQuery();
        if(rs.next()){
            System.out.println("Inside RS");
            byte[] bytearray = new byte[1048576];
            int size=0;
            sImage = rs.getBinaryStream(4);
            response.reset();
            response.setContentType("image/jpeg");
            while((size = sImage.read(bytearray)) != -1 ){
                response.getOutputStream().
                write(bytearray,0,size);
            }
        }

    } catch (Exception e){
        e.printStackTrace();
    }
}
}

这是我的jsp页面imagedemo.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <h1>Hello World!</h1>


        <img src="DisplayServlet?id=1" height="150px" width="150px" alt="ProfilePic">

    </body>
</html>

JSP页面
你好,世界!
最后,这是我的xml文件

<servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

<servlet>
    <servlet-name>DisplayServlet</servlet-name>
    <servlet-class>DisplayServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>DisplayServlet</servlet-name>
    <url-pattern>/DisplayServlet/*</url-pattern>
</servlet-mapping>

行动
*.做
显示servlet
显示servlet
显示servlet
/显示servlet/*

您只选择了一列:

stmt = conn.prepareStatement("select photo from contacts where id=" + imageId);
但后来尝试检索第4个:

sImage = rs.getBinaryStream(4);
在编写循环之后,没有flush()调用。
另外,最好使用池中的连接,而不是直接从driverManager实例化连接,并在工作结束后关闭连接。

::谢谢。它解决了我的问题:)非常感谢,先生。