如何使用 Struts 2 从服务器检索图像给用户

问题描述 投票:0回答:3

我有一个

Product
实体,它有一个
imageUrl
字符串字段。

从用户处获取的产品图片将保存在目录:

System.getProperty("user.home") + "shop/data/product/"

当用户想要看到一些

Product
时,我需要从
"user.home"+...
获取此图像到JSP页面。

我尝试将图像读入字节数组,将其转换为Base64编码,然后在JSP中引用,如下所示:

<img alt="image from user home" src="data:image/png, base64;${requestScope.image}">

但是这个解决方案不起作用,据我了解,它对图像大小有限制。

你能建议我一种如何做这样的事情的方法吗?

java image jsp struts2 io
3个回答
2
投票

试试这个(我认为你有一些错字)

<img alt="image from user home" src="data:image/png;base64,${requestScope.image}">

还可以使用此网站:http://www.askapache.com/online-tools/base64-image-converter/ 确保您输出的 Base64 代码正确。


2
投票

您可以使用从文件返回图像字节的操作,而不是使用将内容长度增加两倍并减慢页面加载速度的 Base64 编码/解码。它可以是一个数据库,这样它应该从

Blob
获取字节。

在使用

<img>
属性的
src
标签中,可以包含返回响应的操作的 URL,其中包含标头
Content-Type: image/jpeg
和写入正文的字节。

这是一个

ImageAction
的示例,它提供来自文件系统的图像。 这叫

Struts 2 动态图像示例。

这是代码

ImageAction.java

@Result(type = "stream", params = {"contentType", "${type}"})
public class ImageAction extends ActionSupport implements ServletRequestAware {
 
    byte[] imageInByte = null;
    String imageId;
 
    private HttpServletRequest servletRequest;

    private final static String type = "image/jpeg";

    public getInputStream() { return new ByteArrayInputStream(getCustomImageInBytes()); }

    public String getType() { return type; }
 
    private String getFilename() {
        return this.filename;
    }

      
    public String getImageId() {
        return imageId;
    }
 
    public void setImageId(String imageId) {
        this.imageId = imageId;
    }
 
    public ImageAction() {
        System.out.println("ImageAction");
    }
      
    public byte[] getCustomImageInBytes() {
 
        System.out.println("imageId" + imageId);
 
        BufferedImage originalImage;
        try {
            originalImage = ImageIO.read(getImageFile(this.imageId));
            // convert BufferedImage to byte array
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(originalImage, "jpeg", baos);
            baos.flush();
            imageInByte = baos.toByteArray();
            baos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        return imageInByte;
    }
 
    private File getImageFile(String imageId) {
        String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
        File file = new File(filePath + "/Image/", imageId);
        System.out.println(file.toString());
        return file;
    }
 
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.servletRequest = request;
 
    }
 
}    

此操作应该具有由

convention-plugin
创建的配置。所以它可以像这样在 HTML 中使用

<img src="<s:url action='Image?imageId=%{imageId}' />" alt=""/>

1
投票

所以Alireza Fattahi 是对的,我的代码中有错误。第一个是

img
标签中的拼写错误(参见 Alireza Fattahi 的回答),第二个是在将图像读取到字节数组之后

byte[] image = ...;

我用过

Base64.getEncoder().encode(image);

而不是

Base64.getEncoder().encodeToString(image));

最终这个方法可以返回 Base64 编码的图像。如果有更好的选择 - 请留下评论和答案。

© www.soinside.com 2019 - 2024. All rights reserved.