2

我正在尝试将我从 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为它设置了。图像只有一个损坏的图像图标。当我尝试在新选项卡中读取缓存的响应时,它可以访问但不会再次呈现。

你能指出我做错了什么吗?已经尝试了很多,但没有运气。

4

2 回答 2

0

我认为问题不在于流提前关闭,我认为会妨碍下载的问题,看这里: https ://docs.openkm.com/kcenter/view/sdk4j-1.1/document-samples .html#getContent

从服务器端(OpenKM 和您的用户界面之间的中间),问题通常是:

//response.setContentLength(is.available()); // Cause a bug, because at this point InputStream still has not its real size.

你应该使用

response.setContentLength(new Long(doc.getActualVersion().getSize()).intValue());
于 2017-06-17T09:05:16.590 回答
0

通过替换request-promiserequest包来解决这个问题,以便向 java BE 发出这个请求,并将回复直接传递到角度 FE 的包装响应中:

let reply = request(options);
reply.pipe(res);
于 2017-07-03T10:10:48.223 回答