Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Thymeleaf显示Base64字符串图像_Thymeleaf_Apache Commons Codec - Fatal编程技术网

使用Thymeleaf显示Base64字符串图像

使用Thymeleaf显示Base64字符串图像,thymeleaf,apache-commons-codec,Thymeleaf,Apache Commons Codec,我将jpg图像存储在数据库中(作为字节数组)。我希望在显示在网页上之前避免掉到文件系统上 单元测试表明,数据库存储和检索工作正常,没有损坏。FIE可以从数据库中提取并转换回jpg文件 图像已转换为bytearray,并使用以下代码存储在数据库中: public static byte[] getImageAsBytes(BufferedImage buffer) throws IOException { ByteArrayOutputStream baos = new ByteArray

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

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

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

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类,它包含从数据库检索到的字节数组。此类还有一个将bytearray转换为base64字符串的方法

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;
}
}
My Thymeleaf磁贴然后调用转换方法generateBase64Image():


好的,事实证明这非常简单,我在提问后2分钟内就解决了这个问题,但我打赌其他人也会有同样的问题

答案是不要使用URLSafe


它与
encodeBase64String()

一起工作解决了这个问题,你应该得到+1感谢:)显然,Thymeleaf现在不接受byte[],它在html文件中转换为Base64,所以我必须
Base64.getEncoder().encodeToString(byte[])
才能将它设置为上下文变量你能在这里发布新的答案吗?这个问题似乎引起了大量的讨论。保持它的最新状态会有帮助