反正是有寫圖像中的freemarker而不是給的環節Feemarker寫作圖片的HTML
<img src="${pathToPortalImage}
注:不能,我們使用的FreeMarker otputstream什麼?
反正是有寫圖像中的freemarker而不是給的環節Feemarker寫作圖片的HTML
<img src="${pathToPortalImage}
注:不能,我們使用的FreeMarker otputstream什麼?
您可以直接在html img
標記中將圖像作爲base64嵌入。
要將圖像轉換爲基準64,您可以使用Apache Commons (codec)。
下面是使用Apache下議院IO +編解碼器解決方案(但是你可以不用,如果你想這樣做):
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}" />
上面的一個很好的例子。但隨着JAVA 8,我們可以做這樣的事情:
Path path = Paths.get("image.png");
byte[] data = Files.readAllBytes(path);
byte[] encoded = Base64.getEncoder().encode(data);
String imgDataAsBase64 = new String(encoded);
String imgAsBase64 = "data:image/png;base64," + imgDataAsBase64;
freemarker的只是一個模板,圖像是圖像,則必須提供其網址,本地文件URL的任何網頁的URL。 – Ankit
我需要這樣的東西http://www.conandalton.net/2008/10/sending-binary-data-from-freemarker.html – Janith