用Thymeleaf显示Base64String图像

编程入门 行业动态 更新时间:2024-10-27 02:23:54
本文介绍了用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.apachemons.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图像

本文发布于:2023-11-26 19:29:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Thymeleaf   Base64String

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!