Newsletter

Download Files from (JAX-RS) RESTful Web Service

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

RestServiceFileDownloadJava4s.java

Explanation

  • 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 rb.build() [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.

Output

You Might Also Like

  ::. About the Author .::

Java4s_Author
- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

5 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.

  5. dinesh katragadda says:

    so valuable info

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Hibernate Spring Struts
Contact | About Us | Privacy Policy | Advertise With Us

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