我正在尝试将我从 Java 服务获得的图像渲染为InputStream
,通过 NodeJS Express 服务器重新发送它,最后在 Angular4 中渲染它
这就是我所做的:
Java 泽西岛服务:
@GET
@Path("thumbnail")
@ApiOperation(
value = "Gets document preview",
notes = "Gets document preview"
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Preview of the document")
})
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("image/png")
public Response getDocThumbnail(
@ApiParam(value = "Entity UUID", required = true) @FormDataParam("uuid") String uuid
) throws RepositoryException, UnknowException, WebserviceException, PathNotFoundException, DatabaseException, AutomationException, AccessDeniedException, ConversionException, IOException {
RawDocument rawDocument = docCtrl.getDocThumbnail(uuid);
return Response
.ok(rawDocument.getInputStream(), "image/png")
.header("Content-Disposition", "attachment; filename=\" " + rawDocument.getName() + "\"")
.build();
}
控制器看起来像:
public RawDocument getDocThumbnail(String uuid) throws IOException, AccessDeniedException, PathNotFoundException, WebserviceException, RepositoryException, DatabaseException, ConversionException, AutomationException, UnknowException {
return new RawDocument(
okmWebSrv.getOkmService().getThumbnail(uuid, ThumbnailType.THUMBNAIL_LIGHTBOX),
"whatever"
);
}
基本上是调用OpenKM SDK 来检索文档的缩略图
这个 Java 端点是从 NodeJS Express 4.15 调用的,它正在预处理这个 Java 后端的一些请求。这就是我所做的:
...compose request options...
const vedica_res = await rp(options);
let buffered = new Buffer(vedica_res, 'binary');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-disposition': 'attachment;filename=' + 'thumb.png',
'Content-Length': buffered.length
});
return res.end(buffered, 'binary');
最后,Angular4 是这次往返的发起者,我试图像这样渲染图像:
this.rest.send('http://localhost:4200/vedica/api/document/thumbnail', RequestMethod.Get,
{uuid: '19516ea1-657e-4b21-8564-0cb87f29b064'}, true).subscribe(img => {
// this.preview = img
var urlCreator = window.URL;
var url = urlCreator.createObjectURL(img);
this.thumb.nativeElement.src = url;
})
收到的“img”是一个Blob {size: 81515, type: "image/png"}
. <img #thumb/>
控制台没有显示错误,但在标签中没有呈现图像。但我可以看到它src=blob:http%3A//localhost%3A3000/4cf847d5-5af3-4c5a-acbc-0201e60efdb7
为它设置了。图像只有一个损坏的图像图标。当我尝试在新选项卡中读取缓存的响应时,它可以访问但不会再次呈现。
你能指出我做错了什么吗?已经尝试了很多,但没有运气。