简体   繁体   English

如何使用Struts2在JSP中显示byte []图像

[英]How to display byte[] image in JSP using Struts2

I am trying to display a byte array image in a JSP using Struts2. 我正在尝试使用Struts2在JSP中显示字节数组图像。 First of all, the image was uploaded as follows: 首先,图像上传如下:

saveImage.jsp saveImage.jsp

<s:form action="saveImage" enctype="multipart/form-data" method="POST">
   <s:file name="file"/>
</s:form>

struts.xml 在struts.xml

<action name="saveImage" class="com.actions.ImageAction" method="save">
        <result name="success" type="redirect">listImages</result>
</action>
<action name="listImages" class="com.actions.ImageAction" method="list">
        <result name="success">listImages.jsp</result>
</action>

ImageAction.class ImageAction.class

public class ImageAction extends ActionSupport  implements ModelDriven<Image> {

    private Image = new Image();
    private File file;
    private List<Image> imageList = new ArrayList<>();

    public Image getImage() {
       return image;
    }
    public void setImage(Image image) {
       this.image = image;
    } 
    public File getFile() {
            return file;
    } 
    public List<Image> getImageList() {
        return imageList;
    }

    public void setImageList(List<Image> imageList) {
        this.imageList = imageList;
    }

Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);

Session session = null;

    @SkipValidation
    public String save() {
        try {
             session = sessionFactory.openSession();
             session.beginTransaction();

            byte[] byteFile = new byte[(int) file.length()];
                try {
                    FileInputStream fs = new FileInputStream(file);
                    fs.read(byteFile);
                    fs.close();
                } catch (Exception e) {}
            image.setImageData(byteFile);

             session.saveOrUpdate(image);
             session.getTransaction().commit()
        } catch (Exception e) {
            session.getTransaction().rollback();
        } finally {
            session.close();
        }
        return SUCCESS;
    }

    @SkipValidation
    public String list() {
        try {
            session = sessionFactory.openSession();
            session.beginTransaction();
            imageList = session.createQuery("from Image").list();
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
        } finally {
            session.close();
        }
        return SUCCESS;
    }

@Override
public Image getModel() {
    return image;
}

} }

The Image object has a byte[] imageData property and is saved to DB using Hibernate. Image对象具有byte [] imageData属性,并使用Hibernate保存到DB。 Now, after successfully saving the image byte array to DB, Struts calls the listImages action that shows in a listImages.jsp the list of existing images in DB. 现在,成功后保存图像字节数组DB,Struts的调用listImages的行动,显示了listImages.jsp在DB现有的图像列表。

listImages.jsp listImages.jsp

     <s:if test="imageList.size() > 0">
       <ul>
        <s:iterator value="imageList">
         <li><s:property value="imageData" /></li>
        </s:iterator>
       </ul>
     </s:if>

Using this "property" tag only shows me some string associated with the image byte array, such as "[B@2eeb2d23". 使用此“属性”标签仅显示一些与图像字节数组关联的字符串,例如“ [B @ 2eeb2d23”。 My question is: how do I display a clickable thumbnail or even a URL instead of that string? 我的问题是:如何显示可点击的缩略图或什至是URL而不是该字符串? And then, when I click the thumbnail or URL, to display the full size image. 然后,当我单击缩略图或URL时,显示完整尺寸的图像。 I tried many different approaches but none of them worked. 我尝试了许多不同的方法,但没有一个起作用。 Maybe it's something I do wrong at uploading the file, or saving it on DB... please provide a working solution. 在上传文件或将其保存在数据库上时,可能是我做错了。请提供有效的解决方案。 Thanks. 谢谢。

In HTML, images and other resources are not part of the HTML page, but are referenced by it. 在HTML中,图像和其他资源不是HTML页面的一部分,而是由HTML页面引用的。

So, your HTML will need to have <img src="[URL TO THE IMAGE]"/> and your JSP will be something like 因此,您的HTML需要具有<img src="[URL TO THE IMAGE]"/>而您的JSP类似于

<s:iterator value="imageList">
  <img src="[DOWNLOAD_URL]?id=${imageId}"/>
</s:iterator>

where DOWNLOAD_URL will point to a HttpServlet that serves the image data based in its id. 其中DOWNLOAD_URL将指向一个HttpServlet ,该HttpServlet根据其ID提供图像数据。

Found a partial workaround, using this solution: http://java.dzone.com/articles/struts2-tutorial-part-67 使用此解决方案找到了部分解决方法: http : //java.dzone.com/articles/struts2-tutorial-part-67

So basically I'm not saving the image's byte array to DB anymore. 因此,基本上我不再将图像的字节数组保存到DB。 When uploading the image through struts, I only save the image's filename into DB and then I make a copy of the image (since the temporary image will be deleted shortly after the upload action is ended) and save the copy on the server as explained in the provided tutorial above. 通过Struts上载图像时,我只将图像的文件名保存到DB中,然后制作图像的副本(因为临时图像将在上载操作结束后不久被删除),然后按照以下说明将副本保存在服务器上上面提供的教程。 Then, for displaying a certain image, I query it's name from DB and then retrieve it from the location where I copied it the first time, when I uploaded it on the server. 然后,为了显示某个图像,我从数据库中查询了它的名称,然后在将其上传到服务器上时从第一次复制它的位置检索了它。 Thanks SJuan76 for the hint. 感谢SJuan76的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM