【问题标题】:How to retrieve images from server to user using Struts 2如何使用 Struts 2 从服务器检索图像到用户
【发布时间】:2016-01-19 03:43:43
【问题描述】:

我有一个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}">

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

你能建议我做这种事情的方法吗?

【问题讨论】:

  • 为什么你把它读成 byteSream?为什么不将其用作静态资源。您可以将System.getProperty("user.home") + "shop/data/product/"+imageFileName 设置为请求属性,然后将此路径添加到src 属性。或者如果是因为图像在服务器上,那么给一个相对 url 到服务器上的路径
  • @Amit.rk3 是的,我已经提到图像存储在服务器中。所以第一个建议是行不通的。你能提供一些解释如何做第二个吗?
  • 所以我猜第二个也行不通。你可能不想检查this answer,它谈到了创建一个单独的 servlet 来动态提供图像
  • @RomanC 我还将尝试您提供的第一个解决方案,并单独采取行动。似乎更合适

标签: java image jsp struts2 io


【解决方案1】:

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

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

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

【讨论】:

  • @Mark,请使用提及站点并测试 ${requestScope.image} 输出是否为正确的 base64 格式。该网站将 Base64 反转为图像
  • 我发现了我的错误,并会尽快发布。收回我的话-您的解决方案有效:)
【解决方案2】:

所以 Alireza Fattahi 是对的,我的代码中有错误。第一个是img 标签中的错字(参见 Alireza Fattahi 的回答),第二个是在将图像读取到字节数组之后

byte[] image = ...;

我用过

Base64.getEncoder().encode(image);

而不是

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

所以最终这种返回 Base64 编码图像的方法有效。如果有更好的选择 - 请留下 cmets 和答案。

【讨论】:

    【解决方案3】:

    有一个ImageAction 的示例,它提供来自文件系统的图像。它被称为 Struts 2 dynamic image example。

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

    在使用src 属性的&lt;img&gt; 标记中,可以包含操作的URL,该操作返回带有标题Content-Type: image/jpeg 和写入正文的字节的响应。

    这是ImageAction的代码:

    @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=darksouls.jpg' />" alt=""/>
    

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 2013-04-27
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多