【问题标题】:How to display an image using tag in JSP如何在 JSP 中使用标签显示图像
【发布时间】:2013-08-02 18:56:23
【问题描述】:

这是在动作类中检索图像的代码。我试过了,但无法显示它我不知道图像处理。 JSP中如何使用标签来显示图片?

public String displayImage()
  {
    Connection con = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    String sql,result="";
    con=JdbcHelper.getConnection();
    if(con!=null)
    {
      try
      {
        sql = "SELECT INPUT_FILE_BLOB FROM message_details where message_id=?";
        preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, messageid);
        resultSet = preparedStatement.executeQuery();
        if(resultSet.next())
        {
          Blob image = resultSet.getBlob("INPUT_FILE_BLOB");
          System.out.println("=============Image2\n" +image);
          int len1 = (int) image.length();
          System.out.println("=============len1\n" +len1);
          byte [] rb1 = new byte[len1];
          InputStream readImg1 = resultSet.getBinaryStream(1);
          try {
            int index1=readImg1.read(rb1, 0, len1);
            System.out.println("index1"+index1);
            response.reset();
            response.setContentType("image/jpg");
            response.getOutputStream().write(rb1,0,len1);
            response.getOutputStream().flush();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
      catch(SQLException e)
      {
        try
        {
          con.rollback();
        }
        catch (SQLException e1)
        {
          result = e1.getMessage();
          e.printStackTrace();
        }
        result = e.getMessage();
        e.printStackTrace();
      }
      finally
      {
        JdbcHelper.close(resultSet);
        JdbcHelper.close(preparedStatement);
        JdbcHelper.close(con);
      }
    }
    else
      result = Constants.SUCCESS;

  }

【问题讨论】:

  • 使用指向该操作 URL 的 img 标签。

标签: java image jsp struts2 blob


【解决方案1】:

您应该通过实现自定义结果类型以在 JSP 上显示图像,以 struts 2 方式进行。

Here is an Example for displaying dynamic image in Stuts2

它通过实现Result 接口创建自定义结果类型。

public class CustomImageBytesResult implements Result {
.
.
.
}

【讨论】:

  • 谢谢....但在该示例中无法找到如何从数据库中获取图像
  • 没有理由使用自定义结果类型;使用“流”结果。连同现有的 Apache Commons 库,它最终只有几行。
  • @Akshobhya:只需编写逻辑以从方法 #getImageFile 中的 DB 获取图像。
【解决方案2】:

从数据库中以Blob 或字节数组形式检索图像后,您将直接写入响应。在这种情况下,您不应该返回 SUCCESS 结果,您需要返回 NONE 结果。然后在 JSP 中,您可以使用 img 标记将图像作为单独的线程访问,该标记将调用您返回图像的操作。

另见How can we display dynamic or static images that can be provided as an array of bytes, 和How to display image in Struts2

【讨论】:

  • 谢谢............在下面的代码中 public class MyAction extends ActionSupport { public String doDefault() { return "myImageResult"; } public byte[] getMyImageInBytes() { .... } public String getMyContentType() { ... } public String getMyContentDisposition() { ... } public int getMyContentLength() { .... } public int getMyBufferSize() { ... } } 这些只是 getter 方法还是我们需要在这些方法中编写代码?
  • 您可以提供具有实际值的代码,它将覆盖结果参数。但是,您也可以在结果中使用常用的 getter 和 setter 来通过 config.xml 设置参数。另一种方法是使用StreamResultByteArrayInputStream 结合使用,它为结果包装字节数组也可以解决您的问题。
猜你喜欢
  • 1970-01-01
  • 2012-07-07
  • 2012-06-28
  • 2015-03-13
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
相关资源
最近更新 更多