0

Here is the code to retrieve image in action class. I have tried it, but not able to display it I don't know about the image processing. How should I use tags in JSP to display the image?

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;

  }
Roman C
  • 49,761
  • 33
  • 66
  • 176
Akshobhya
  • 169
  • 2
  • 16

2 Answers2

0

You should do it in struts 2 way by implementing a Custom Result Type to display image on the JSP.

Here is an Example for displaying dynamic image in Stuts2.

It creates a custom result type by implementing Result interface.

public class CustomImageBytesResult implements Result {
.
.
.
}
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
0

After retrieving an image from the database as Blob or byte array you are writing direct to response. In this case you should not return SUCCESS result, you need to return NONE result. Then in the JSP you could access an image as a separate thread using img tag that will call your action that return an image.

See also How can we display dynamic or static images that can be provided as an array of bytes, and How to display image in Struts2.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • thanks...........in below code 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() { ... } } these are just getter methods or we need to write code inside these methods? – Akshobhya Aug 02 '13 at 07:47
  • You could provide a code with actual values, it will override the result parameters. But, you could also use usual getters and setters in the result to set parameters there via config. An alternative approach is the use of the `StreamResult` with conjunction of `ByteArrayInputStream` that wraps the byte array for the result could also solve your problem. – Roman C Aug 02 '13 at 09:05