如何关闭REST服务流? [英] How to close a Stream of a REST Service?

查看:94
本文介绍了如何关闭REST服务流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个Java REST服务,该服务将返回输入流作为响应。我的问题是,在客户收到整个流后,我不知道如何关闭流。我正在使用Java和CXF。谢谢

I need to make a java REST service that will return an inputstream as a response. My problem is that I don't know how to close the stream after the client receives the entire stream. I'm using Java and CXF. Thanks

@GET
@Path("/{uuid}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getAttachmentByUuid(@PathParam("uuid")String uuid)
{
 //getting inputstream from AWS S3
 InpputSream is=getStreamFromS3(uuid);
 return Response.status(Response.Status.OK).entity(is).build();
 // will this "is" stream causes memory leak,, do I have to close it. Client side is not controlled by me
}


推荐答案

JAX-RS使用Java Servlet实现。如果使用CXF,请使用 CXFServlet 。您的流将使用servlet接口的 HttpServletResponse 发送到客户端

JAX-RS is implemented using Java servlets. In case of CXF is used CXFServlet. Your stream will be sent to client using the HttpServletResponse of the servlet interface

public void doGet(HttpServletRequest request, HttpServletResponse response)

您不应关闭流源 HttpServletResponse )(如果尚未创建)。这是容器的责任,您可以干扰请求的生命周期

You should not close an stream source (HttpServletResponse) if you have not created it. It is responsability of the container, and you can interfere with the life cycle of the request

另请参见

See also Is is necessary to close the input stream returned from HttpServletRequest?

这篇关于如何关闭REST服务流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆