用Thymeleaf显示Base64String图像 [英] Showing Base64String Image with Thymeleaf

查看:324
本文介绍了用Thymeleaf显示Base64String图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将jpg图像存储在数据库中(作为字节数组).我想避免在显示在网页上之前掉入文件系统.

I am storing jpg images in a database (as byte array). I want to avoid dropping onto filesystem before showing on a web page.

单元测试表明,数据库存储和检索工作正常,没有损坏. Fies可以从数据库中提取出来并转换回jpg文件

Unit Tests show that database storage and retrieval are working without corruption. Fies can be extracted from database and converted back to jpg file

图像已转换为字节数组,并使用以下代码存储在数据库中:

The image was converted to bytearray and stored in database with the following code:

public static byte[] getImageAsBytes(BufferedImage buffer) throws IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(buffer, "jpg", baos);
    baos.flush();
    byte[] imageInByte = baos.toByteArray();
    baos.close();
    return imageInByte;

}

我有一个ViewWrapperMediaImage类,其中包含从数据库中检索到的字节数组.此类还具有将字节数组转换为base64 String的方法.

I have a class ViewWrapperMediaImage that contains the byte array retrieved from the database. This class also has a method that converts the bytearray to base64 String.

package jake.prototype2.controller.viewwrapper;

import org.apache.commons.codec.binary.Base64;

import jake.prototype2.model.assessment.MediaImage;
import jake.prototype2.model.assessment.TestStructureException;
import jake.prototype2.model.structure.InterfacePersistenceBean;

public class ViewWrapperMediaImageCreate extends ViewWrapperTestContentElementCreate
{

private byte[] image;

protected String mediaFileName;

private static final long serialVersionUID = 4181515305837289526L;

public ViewWrapperMediaImageCreate(InterfacePersistenceBean persistenceBean) throws TestStructureException
{
    ....
    }
}

public byte[] getImage()
{
    return image;
}

public String generateBase64Image()
{
    return Base64.encodeBase64URLSafeString(this.getImage());
}

public void setImage(byte[] image)
{
    this.image = image;
}

public String getMediaFileName()
{
    return mediaFileName;
}

public void setMediaFileName(String mediaFileName)
{
    this.mediaFileName = mediaFileName;
}
}

然后,我的Thymeleaf磁贴调用转换方法generateBase64Image():

My Thymeleaf tile then calls the conversion method generateBase64Image():

<img  th:src="@{'data:image/jpeg;base64,'+${vwNewTestContentElement.generateBase64Image()}}" />

它不起作用.

生成的html源如下:

The generated html source is as follows:

 &lt; img src="data:image/jpeg;base64,_9j_4AAQSkZJRgABAgAAAQABAAD_2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL_2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL_wAARCADhASwDASIAAhEBAxEB_8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL_8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4-Tl5ufo6erx8vP09fb3-....

任何提示将不胜感激

推荐答案

好,事实证明这很简单,我在问问题2分钟之内就解决了,但我敢打赌其他人也会遇到同样的问题.

OK, it turns out this is dead easy, I solved within 2 mins of asking the question, but I bet others will have the same question.

答案是不要使用URLSafe

The answer is don't use URLSafe

它与encodeBase64String()

这篇关于用Thymeleaf显示Base64String图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆