displaying Blob (image) in jsp page using struts 2 and hibernate

前端 未结 1 1378
北恋
北恋 2021-01-24 18:05

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

相关标签:
1条回答
  • 2021-01-24 19:07

    Finally I solved it, for future googlers :

    Add this line to jsp,

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

    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;
      }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题