JAX-RS (REST)No Comments

In JAX-RS, for user to download a file, annotate the method with @Produces(“text/plain”) :

  1. Put @Produces(“text/plain”) on service method, with a Response return type. It means the output is a text file.
  2. Set “Content-Disposition” in Response header to tell browser pop up a download box for user to download.
  • Download File in JAX-RS

See a full example to download a text file in JAX-RS.

  • Download another File Type

If you want to download another file types, as you might noticed just change @Produces(FILE_TYPE) as some FILE_TYPE :

  1. “application/pdf”
  2. “application/vnd.ms-excel”
  3. “image/image-type”
  4. “text/plain”
  • Result

Deploy above JAX-RS service, access this URI pattern : “/file/get“.

Figure : Text file “c:\\test.log” from server is prompt for user to download, with a new file name “file_from_server.log


Be the first to post a comment.

Add a comment