且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用struts 2和hibernate在jsp页面中显示Blob(图像)

更新时间:2023-12-02 13:16:28

最后我解决了这个问题,对于未来的googlers:

Finally I solved it, for future googlers :

将此行添加到jsp,

 <img src="<s:url value="YourImageShowAction" />" border="0"
 width="100" height="100">

这是 ShowImageAction 类:请注意execute方法是void,所以没有重定向

and this is ShowImageAction class : note that the execute method is void, so no redirection


import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.raysep.maxlist.domain.post.image.Image;

public class ShowImageAction {

  private static byte[] itemImage;

  public static void execute() {

      try {

          Image slika = Image.fetchOne();

          HttpServletResponse response = ServletActionContext.getResponse();
          response.reset();
          response.setContentType("multipart/form-data"); 

          itemImage = slika.getImage().getBytes(1,(int) slika.getImage().length());

          OutputStream out = response.getOutputStream();
          out.write(itemImage);
          out.flush();
          out.close();

      } catch (SQLException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }


  }

  public byte[] getItemImage() {
      return itemImage;
  }

  public void setItemImage(byte[] itemImage) {
      this.itemImage = itemImage;
  }


}