无法通过从MIMQL获取值通过Servlet在HTML中显示多个图片

时间:2015-02-13 10:08:39

标签: java jsp java-ee servlets

我想连续显示多张图片输出文件,即HTML

但我无法在HTML中显示多张图片。

以下是我的代码:

@WebServlet("/retrieveServlet")
public class ImageRetrieveDBServlet extends HttpServlet {

 // database connection settings
 private String dbURL = "jdbc:mysql://localhost:3306/imagetest"; //my database name is imagetest
 private String dbUser = "root";
 private String dbPass = "root";

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

    Connection conn = null; // connection to the database
    String message = null;  // message will be sent back to client

    try {
        response.setContentType("image/jpeg");  
        // connects to the database
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        conn = DriverManager.getConnection(dbURL, dbUser, dbPass);

        // constructs SQL statement
        String sql = "select * from contacts";
        PreparedStatement statement = conn.prepareStatement(sql);

        // sends the statement to the database server
        ResultSet resultSet = statement.executeQuery();

        while(resultSet.next()) {
          response.getOutputStream().write(resultSet.getBytes("photo"));
          response.getOutputStream().flush();
        }

    }
    catch (SQLException ex) {
        message = "ERROR: " + ex.getMessage();
        ex.printStackTrace();
    } finally 
    {
        if (conn != null) 
        {
            // closes the database connection
            try 
            {
                conn.close();
             } catch (SQLException ex) 
             {
                ex.printStackTrace();
             }
         }
    }
}

}

  

以上代码仅用于显示一个图像,而不管是否。数据库中的记录。

这种方法是否正确检索以字节格式存储的图像?

0 个答案:

没有答案