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


Warning: stream_socket_client(): unable to connect to http://browscap.org/version (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in /home/javasail/public_html/wp-content/plugins/wp-statistics/vendor/browscap/browscap-php/src/phpbrowscap/Browscap.php on line 1366

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /home/javasail/public_html/wp-content/plugins/wp-statistics/vendor/browscap/browscap-php/src/phpbrowscap/Browscap.php on line 769