Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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_Image_Jsp_Servlets - Fatal编程技术网

Java 如何通过Servlet从数据库中检索图像并在JSP中显示?

Java 如何通过Servlet从数据库中检索图像并在JSP中显示?,java,image,jsp,servlets,Java,Image,Jsp,Servlets,我的ImageDAO看起来像这样: public InputStream getPhotos(Long userid) throws IllegalArgumentException, SQLException, ClassNotFoundException { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultset = null; Data

我的ImageDAO看起来像这样:

public InputStream getPhotos(Long userid) throws 
  IllegalArgumentException, SQLException, ClassNotFoundException {

  Connection connection = null;
  PreparedStatement preparedStatement = null;
  ResultSet resultset = null;
  Database database = new Database();
  InputStream binaryStream = null;

  try {

    connection = database.openConnection();
    preparedStatement = connection.prepareStatement(SQL_GET_PHOTO);                  
    preparedStatement.setLong(1, userid);
    preparedStatement.executeUpdate();

    while(resultset.next()) {
      binaryStream = resultset.getBinaryStream(4);
    }

  } catch (SQLException e) {
      throw new SQLException(e);
  } finally {
      close(connection, preparedStatement, resultset);
  }
  return binaryStream;
}
我的ImageServlet如下所示:

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

  // Getting user id from session
  HttpSession session = request.getSession(false);
  Long userid = (Long) session.getAttribute("user");    

  try {

      InputStream photoStream = imageDAO.getPhotos(userid);

      // Prepare streams.
      BufferedInputStream input = null;
      BufferedOutputStream output = null;

      try {

      // Open streams
      input = new BufferedInputStream(photoStream, DEFAULT_BUFFER_SIZE);
      output = new BufferedOutputStream(response.getOutputStream(),
                                                   DEFAULT_BUFFER_SIZE);

      // Write file contents to response.
      byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
      int length;
      while ((length = input.read(buffer)) > 0) {
          output.write(buffer, 0, length);
      }

      } finally {
          output.close();
          input.close();
      }

      //Redirect it to profile page
      RequestDispatcher rd = request.getRequestDispatcher
                            ("/webplugin/jsp/profile/photos.jsp");
      rd.forward(request, response);


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

}
我的JSP图像src应该是什么样子

<img src="What to put here">

披露:

servlet代码从此链接复制

问题:

  • 如何从ImageServlet检索JSP中的图像。有人说要把
    。但我不知道这意味着什么
  • 上述方法是从数据库检索图像的正确方法吗?还是有更好的办法
  • 编辑:我的Web.xml如下所示

    <servlet>
      <servlet-name>Photo Module</servlet-name>
      <servlet-class>app.controllers.PhotoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>Photo Module</servlet-name>
      <url-pattern>/Photos</url-pattern>
    </servlet-mapping>
    
    
    照片模块
    app.controllers.PhotoServlet
    照片模块
    /照片
    
    附上您的web.xml以查看您(如果您有)如何映射您的servlet,这样我们就可以为您提供URL

    xml是将URL与servlet绑定的方法

    将缓冲输出转换为Servlet输出流的方法是将数据写入输出的正确且良好的方法。不过,最好使用过滤器来缓存输出,而不是一直写入响应

    更新: 根据您提到的源博客和其中的web.xml,正确的url应该是


    其中my_image.jpg是上载图像的示例名称。

    附加您的web.xml以查看您(如果您有)是如何映射servlet的,因此我们可以为您提供URL

    xml是将URL与servlet绑定的方法

    将缓冲输出转换为Servlet输出流的方法是将数据写入输出的正确且良好的方法。不过,最好使用过滤器来缓存输出,而不是一直写入响应

    更新: 根据您提到的源博客和其中的web.xml,正确的url应该是


    其中my_image.jpg是上载图像的示例名称。

    如果您注册了图案的serlvet
    /Photos

    <img src="/yourContextRoot/Photos" />
    

    如果您为图案注册serlvet
    /Photos

    <img src="/yourContextRoot/Photos" />
    

    HTML
    元素的
    src
    应该只指向URL。URL是与您在浏览器地址栏中输入的网址类似的网址。servlet可以通过
    web.xml
    映射到特定的URL模式上,这样当您调用与servlet映射匹配的URL时,就会调用servlet。另见

    您已将servlet映射到
    /Photos
    的URL模式上。输入URL,如

    在浏览器中,地址栏应显示图像。因此,基本上,假设JSP在相同的上下文路径中运行,应该这样做:

    这样,浏览器就可以理解它是JPG图像,并将其显示为JPG图像。有关设置标题的正确方法,请参见您链接的博客

    另一个问题是您正在调用
    request.getSession(false)
    ,当没有会话时,它可能返回
    null
    。但你不是在下一行检查!所以要么使用

    response.setContentType("image/jpeg");
    
    这样它就永远不会
    null
    ,也不会添加

    HttpSession session = request.getSession();
    

    您希望对
    userId
    photoStream
    执行相同的操作。如果没有,则显示默认图像或返回404。

    HTML
    元素的
    src
    应该只指向URL。URL是与您在浏览器地址栏中输入的网址类似的网址。servlet可以通过
    web.xml
    映射到特定的URL模式上,这样当您调用与servlet映射匹配的URL时,就会调用servlet。另见

    您已将servlet映射到
    /Photos
    的URL模式上。输入URL,如

    在浏览器中,地址栏应显示图像。因此,基本上,假设JSP在相同的上下文路径中运行,应该这样做:

    这样,浏览器就可以理解它是JPG图像,并将其显示为JPG图像。有关设置标题的正确方法,请参见您链接的博客

    另一个问题是您正在调用
    request.getSession(false)
    ,当没有会话时,它可能返回
    null
    。但你不是在下一行检查!所以要么使用

    response.setContentType("image/jpeg");
    
    这样它就永远不会
    null
    ,也不会添加

    HttpSession session = request.getSession();
    

    您希望对
    userId
    photoStream
    执行相同的操作。如果没有,则显示默认图像或返回404。

    谢谢您的详细回答!!!另请看@Kunal:你读过这个问题吗?谢谢你的详细回答!!!另请看@Kunal:你读过这个问题了吗?你能解释一下过滤缓存是如何帮助你的,而不是总是写响应吗。这正是我要找的地方。你能解释一下吗?你能解释一下过滤缓存是如何帮助你而不是总是写响应的吗。这正是我要找的地方。你能帮我解释一下吗?