如何使用 Struts 2 从服务器检索图像给用户
How to retrieve images from server to user using Struts 2
我有一个 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}">
但是这个解决方案不起作用,据我所知,它对图像大小有限制。
你能建议我如何做这样的事情吗?
试试这个(我想你有一些错字)
<img alt="image from user home" src="data:image/png;base64,${requestScope.image}">
也可以使用此站点:http://www.askapache.com/online-tools/base64-image-converter/ 以确保您的输出 Base64 代码正确无误。
所以 Alireza Fattahi 是对的,我的代码有错误。第一个是 img
标签中的拼写错误( 参见 Alireza Fattahi 的回答),第二个是将图像读取到字节数组后
byte[] image = ...;
我用过
Base64.getEncoder().encode(image);
而不是
Base64.getEncoder().encodeToString(image));
所以最终这种返回 Base64 编码图像的方法奏效了。如果有更好的选择,请留言和回答。
有一个 ImageAction
的示例,它提供来自文件系统的图像。它被称为
Struts 2 dynamic image example。
您可以使用从文件返回图像字节的操作,而不是将内容长度增加两倍并减慢页面加载速度的 base64 encoding/decoding。它可以是一个数据库,这样它应该从 Blob
中获取字节。
在使用 src
属性的 <img>
标签中,可以包含 URL 到 returns 响应 header [=16] 的操作=] 和字节写入 body.
这是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=""/>
我有一个 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}">
但是这个解决方案不起作用,据我所知,它对图像大小有限制。
你能建议我如何做这样的事情吗?
试试这个(我想你有一些错字)
<img alt="image from user home" src="data:image/png;base64,${requestScope.image}">
也可以使用此站点:http://www.askapache.com/online-tools/base64-image-converter/ 以确保您的输出 Base64 代码正确无误。
所以 Alireza Fattahi 是对的,我的代码有错误。第一个是 img
标签中的拼写错误( 参见 Alireza Fattahi 的回答),第二个是将图像读取到字节数组后
byte[] image = ...;
我用过
Base64.getEncoder().encode(image);
而不是
Base64.getEncoder().encodeToString(image));
所以最终这种返回 Base64 编码图像的方法奏效了。如果有更好的选择,请留言和回答。
有一个 ImageAction
的示例,它提供来自文件系统的图像。它被称为
Struts 2 dynamic image example。
您可以使用从文件返回图像字节的操作,而不是将内容长度增加两倍并减慢页面加载速度的 base64 encoding/decoding。它可以是一个数据库,这样它应该从 Blob
中获取字节。
在使用 src
属性的 <img>
标签中,可以包含 URL 到 returns 响应 header [=16] 的操作=] 和字节写入 body.
这是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=""/>