0

アクションクラスで画像を取得するコードは次のとおりです。やってみたのですが表示できません 画像処理がわかりません。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;

  }
4

2 に答える 2

0

カスタム結果タイプを実装してJSPに画像を表示することにより、Struts 2の方法でそれを行う必要があります。

Stuts2 で動的画像を表示する例を次に示します。

インターフェイスを実装することにより、カスタム結果タイプを作成しResultます。

public class CustomImageBytesResult implements Result {
.
.
.
}
于 2013-08-01T17:57:54.850 に答える
0

またはバイト配列としてデータベースから画像を取得した後、Blob応答に直接書き込みます。この場合、SUCCESS の結果を返すべきではありません。NONE の結果を返す必要があります。img次に、JSP で、画像を返すアクションを呼び出すタグを使用して、別のスレッドとして画像にアクセスできます。

バイト配列として提供できる動的または静的画像を表示する方法およびStruts2 で画像を表示する方法も参照してください。

于 2013-08-01T18:35:48.830 に答える