使用struts 2和hibernate在jsp页面中显示Blob(图像) [英] displaying Blob (image) in jsp page using struts 2 and hibernate

查看:165
本文介绍了使用struts 2和hibernate在jsp页面中显示Blob(图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法将我的mysql数据库中的图像存储为Blob。 (我也在使用休眠)
现在我正在尝试加载该图像并将其发送到jsp页面,以便用户可以查看图像。

i managed to store an image in my mysql database as Blob. (i am also using hibernate) now i am trying to load that image and send it on a jsp page so the user can view the image.

这是我的struts 2动作类

This is my struts 2 action class


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Blob;
import org.hibernate.Hibernate;

import domain.post.image.Image;



public class FileUploadAction {

  private File file;

  @SuppressWarnings("deprecation")
  public String execute() {

      try {
          System.out.println(file.getPath());
          Image image = new Image();
          FileInputStream fi = new FileInputStream(file);

          Blob blob = Hibernate.createBlob(fi);
          image.setImage(blob);
          image.save();

      } catch (FileNotFoundException e) {

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

          e.printStackTrace();
      }
      return "success";
  }

  public File getFile() {
      return file;
  }

  public void setFile(File file) {
      this.file = file;
  }


这是我的Image类


public class Image extends AbsDBObject<Object> {


  private static final long serialVersionUID = 1L;
  private static Logger logger = Logger.getLogger(Image.class);
  private Blob image;
  private String description;

//Getters and Setters

}


请问我应该在动作类,jsp页面和struts.xml中放置什么才能显示存储的图像?

would you please tell me what should i put in an action class, jsp page and struts.xml in order to showing the stored image?

推荐答案

最后我解决了这个问题,对于未来的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;
  }


}


这篇关于使用struts 2和hibernate在jsp页面中显示Blob(图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆