Get all updates of Java4s
Follow Us: Email Newsletter

Download Files from (JAX-RS) RESTful Web Service

Web Services » On Jul 10, 2014 { 4 Comments } By Sivateja

In this article i will show you how to download files from your JAX-RS  web service.  Downloading files from restful is easier compared to upload :-), however i will give you both examples.  We can download any type of files from the RESTful web services, its just a matter of changing @produces annotation. For example..

We should annotate our method with

@Produces(“text/plain“) If you are expecting Text file as response
@Produces(“image/your image type[.jpg/.png/.gif]”) for downloading any Image files
@Produces(“application/pdf“) for downloading PDF files

Lets discuss these three scenarios with an example.

Required Files


  • Our intention is to download the TEXT file from JAX-RS, for that we need to annotate our method with @Produces(“text/plain”) [which i did in line number 24]
  • Once we call the RESTful service, i want to display a pop-up download box for the users to ‘download‘ that file, in order to do that we need to add ‘Content-Disposition‘ header to the response
  • But in the Response class we don’t have any option to add the headers, so firstly i have created ‘ResponseBuilder‘ object [ line number 31, because in ResponseBuilder class we have direct method to add the headers], and added ‘Content-Disposition‘ to the header.
  • Finally called [at line number 33], this will create a Response instance from the current ResponseBuilder object (rb) and returns
  • We can also get the output by simply writing the lines 16-19 but it wont shows download pop-up box :-)
  • You can enable, line numbers 25,26 if your file is Image & PDF respectively

Same thing will happen in case of Images/PDF or other file formats.


You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


4 Responses to “Download Files from (JAX-RS) RESTful Web Service”
  1. Venkata Sriram says:

    Thanks sir.your articles are so much informative.Thanks

  2. Gireesh says:

    Can you provide how can i download multiple files and its meta data

  3. venkata rami reddy says:

    u r applications are so use full.

    but u will try to post the following type of applications.

    title: create a restful webservice with spring mvc that add and returns a list of cars.

  4. Munisekhar says:

    Hi Sir,

    This tutorial is good,But i need the SOAP Web Service please…Help Me.


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2017 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.