1

I am using jersey API in my project. 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. I want this data to be in a file and in a downloadable format.

I also tried @Produces(MediaType.APPLICATION_OCTET_STREAM).

Please correct me if am doing wrong

svarog
  • 9,477
  • 4
  • 61
  • 77
  • possible duplicate, http://stackoverflow.com/questions/12239868/whats-the-correct-way-to-send-a-file-from-rest-web-service-to-client – svarog Dec 21 '15 at 12:44

1 Answers1

0

I think you are not sending the response you have build.

first you're using a ResponseBuilder to build the resposne

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

you should return response.build()

also, you should produce octet-stream in your method annotions

@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?

Community
  • 1
  • 1
svarog
  • 9,477
  • 4
  • 61
  • 77
  • i have used response.build() only. it was a typo error and also i have tried using @Produces(MediaType.APPLICATION_OCTET_STREAM). It's not getting download – user3760732 Dec 31 '15 at 07:33