简体   繁体   English

无法使用JERSEY下载文件

[英]File is not getting download using JERSEY

I am using jersey API in my project. 我在我的项目中使用jersey API。 I stuck in a case that file needs to be downloaded but it's not. 我坚持认为需要下载文件,但事实并非如此。 My code is as follow 我的代码如下

@GET
@Produces("application/download")
public Response downloadFile(){ 
    String data = getDatas();
    ResponseBuilder response = Response.ok(data);
    response.header("Content-Disposition","attachment;filename=UserData.txt");
    response.header("charset", "UTF-8");
    return Response.build(); 
}

I have added all the packages, paths are also fine. 我已经添加了所有软件包,路径也很好。 No Error came. 没有错误。

When I call this API, data comes in the response. 当我调用此API时,数据来自响应。 I want this data to be in a file and in a downloadable format. 我希望这些数据以文件形式并以可下载的格式显示。

I also tried @Produces(MediaType.APPLICATION_OCTET_STREAM) . 我还尝试了@Produces(MediaType.APPLICATION_OCTET_STREAM)

Please correct me if am doing wrong 如果做错了请纠正我

I think you are not sending the response you have build. 我认为您没有发送已建立的响应。

first you're using a ResponseBuilder to build the resposne 首先,您使用ResponseBuilder来构建资源库

ResponseBuilder response = Response.ok(data);
response.header("Content-Disposition","attachment;filename=UserData.txt");
response.header("charset", "UTF-8");

then you are returning the static Response.build() (notice the capital R ) object, which is empty 那么您将返回静态的Response.build() (注意大写R )对象,该对象为空

you should return response.build() 您应该返回response.build()

also, you should produce octet-stream in your method annotions 另外,您应该在方法注释中生成octet-stream

@Produces(MediaType.APPLICATION_OCTET_STREAM)

and not 并不是

@Produces("application/download")

refer to this question: what's the correct way to send a file from REST web service to client? 请参考以下问题: 从REST Web服务向客户端发送文件的正确方法是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM