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

标签 java hibernate jakarta-ee struts2 web

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

这是我的 struts 2 Action 类

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;
  }

这是我的图像类

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

}

你能告诉我应该在 action 类、jsp 页面和 struts.xml 中放些什么来显示存储的图像吗?

最佳答案

最后我解决了它,对于 future 的 googlers:

将这一行添加到jsp,

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

这是 ShowImageAction 类:注意 execute 方法是无效的,所以没有重定向

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;
  }


}

关于java - 使用struts 2和hibernate在jsp页面中显示Blob(图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054958/

相关文章:

java - 使用 Java EE 的网关模式

Java 未签名 Applet - 传递 VM 参数 "java.security.policy"被忽略?

java - 如何在 TextView 中显示一些文本指定的时间?

java - 如何选择合适的 JDK JavaEE Spring 版本

java - 使用 Spring,如何获取具有特定实例的类的列表?

java - 跨多个 SDK 版本的 Android ContactsContract 和构建

java - 即使数据库中只插入一行,hibernate 也会自动加载多个重复行

java - Hibernate 注解映射、JPA 孤儿删除

java - 在 Hibernate 中映射具有递增列的复合键

javascript - 无法使用 ajax 获取成功函数中元素的值?