1

mysqlデータベースにBlobとして画像を保存することができました。(私も休止状態を使用しています)現在、ユーザーが画像を表示できるように、その画像を読み込んでjspページに送信しようとしています。

これは私のストラット2アクションクラスです

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に何を入れるべきか教えてください。

4

1 に答える 1

5

最後に私はそれを解決しました、将来のグーグルのために:

この行を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;
  }


}
于 2012-04-07T21:15:52.227 に答える