Java 如何在JSP中使用标记显示图像

Java 如何在JSP中使用标记显示图像,java,image,jsp,struts2,blob,Java,Image,Jsp,Struts2,Blob,下面是在action类中检索图像的代码。我试过了,但无法显示,我不知道图像处理。我应该如何在JSP中使用标记来显示图像 public String displayImage() { Connection con = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; String sql,result=""; con=JdbcHelper.getCo

下面是在action类中检索图像的代码。我试过了,但无法显示,我不知道图像处理。我应该如何在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;

  }

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

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

public class CustomImageBytesResult implements Result {
.
.
.
}

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

另见,
还有。

谢谢。。。。但是在这个例子中,我们无法找到如何从数据库中获取图像,没有理由使用自定义结果类型;使用“流”结果。与现有的Apache Commons库一起,它的长度只有几行。@Akshobhya:只需编写您的逻辑,从方法#getImageFile内的DB获取图像。使用指向该操作URL的img标记。谢谢……。在下面的代码中,公共类MyAction扩展了ActionSupport{public String doDefault(){返回“myImageResult”}public byte[]getMyImageInBytes(){..}public String getMyContentType(){..}public String getMyContentDisposition(){..}public int getMyContentLength(){..}public int getMyBufferSize(){..}这些只是getter方法,或者我们需要在这些方法中编写代码?您可以提供一个带有实际值的代码,它将覆盖结果参数。但是,您也可以在结果中使用常用的getter和setter通过config在那里设置参数。另一种方法是使用
StreamResult
和连词使用
ByteArrayInputStream
封装结果的字节数组也可以解决您的问题。