有没有办法在 freemarker 中写入图像而不是给出链接
<img src="${pathToPortalImage}
注意:我们不能在 freemarker 中使用输出流或其他东西吗?
您可以将图像作为 base64 直接嵌入到 html 中img
tag.
要将图像转换为 Base 64,您可以使用Apache Commons(编解码器) http://commons.apache.org/codec/.
这是使用 Apache Commons IO + Codec 的解决方案(但如果您愿意,也可以不使用):
File img = new File("file.png");
byte[] imgBytes = IOUtils.toByteArray(new FileInputStream(img));
byte[] imgBytesAsBase64 = Base64.encodeBase64(imgBytes);
String imgDataAsBase64 = new String(imgBytesAsBase64);
String imgAsBase64 = "data:image/png;base64," + imgDataAsBase64;
然后传递变量imgAsBase64
进入 Freemarker 上下文,并像这样使用它:
<img alt="My image" src="${imgAsBase64}" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)