2017-06-15 68 views
2

我试图使我从一个Java服务得到了作为InputStream的图像,在Angular4渲染缩略图Angular4

这里通过的NodeJS Express服务器重新发送它,并最终使这是我做的:

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 to retreive document's thumbnail

这个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。图像只是一个破碎的图像图标。 当我尝试在新选项卡中读取缓存响应时,其可访问但不再呈现任何内容。

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

回答

0

解决此通过更换request-promiserequest包提出这个要求的用Java来和管道回复到正确的角度FE的包装响应:

let reply = request(options); 
reply.pipe(res); 
0

我觉得这个问题是不是被早早关门流,我想会的方式问题被下载,看看这里: https://docs.openkm.com/kcenter/view/sdk4j-1.1/document-samples.html#getContent

从服务器端(不知疲倦OpenKM之间中间你用户界面)的问题usualy是:

//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()); 
+0

我真的不知道如何适应我的背景下,这个,不好意思 – greengold

+0

我做我不知道如何用Angular做什么,但用标准servlet解决了我们遇到的问题,正如我解释的那样解决了问题。似乎流停止阅读,我不知道是否变量buffered.length对它有一些影响。我建议在刷新输出流之前,尝试中间的一个步骤,例如将图像保存到文件系统中,并检查它是否正确......稍后尝试刷新一次。 – darkman97i