java - 将base64中的图像从rest服务返回到img src标签

标签 java html image rest base64

我在加载 Base 64 格式的图像时遇到了一些问题。

在前端我有: <img src="/path/to/rest/image">

其余服务代表:

@GET
@PublicResource
@Produces("image/*")
@Path("/path/to/rest/image")
public Response getResource() {

    CacheControl cacheControl = new CacheControl();
    cacheControl.setMaxAge(86400);
    cacheControl.setPrivate(true);
    //image.imputStream() is the stream of the image's base64 representation
    Response.ok(image.getInputStream())).cacheControl(cacheControl)
                    .header("Cache-Control", "max-age=86400").build();
}

它返回正确的 base64,当我在任何解析器中将其解析为图像时,它会显示图像。但img标签仍然显示图像已损坏。 有什么想法如何解决它吗?也许我必须添加一些标题。

最佳答案

将以下文本添加到从休息服务返回的 Base 64 前面

data:image/png; base64, 

关于java - 将base64中的图像从rest服务返回到img src标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436951/

相关文章:

java - 如果缺少参数,Spring 4.1.5 MVC @RequestParam(required = false, value = "somevalue") 失败

java - 从 ValueAnimator 获取动画值而不启动动画

java - 与 MongoDB 相比,使用 Java 驱动程序的 Cassandra Bulk-Write 性能非常糟糕

html - 可滚动内容的响应高度

Python 异步 IO 图像处理

java - intern() 是否在池中创建了一个文字?

angularjs - Google Chrome HTML <select> 元素不选择工作 Google Chrome 移动 View

ios - 有关在TableView中从核心数据显示imageView的一些问题

java - 在android中上传图像到服务器

html - css的对齐问题