Newsletter

Download Files from (JAX-RS) RESTful Web Service

Web Services » on Jul 10, 2014 { 6 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

RestServiceFileDownloadJava4s.java

package com.java4s;
 
import java.io.File;import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
 
@Path("/downnload")
public class RestServiceFileDownloadJava4s {
    
    String path = "c:\\tuts\\java4s.txt";
    
    /*    public File getCustomerDataFile() {
            File file = new File(path);        
            return file;
    }*/
    
    
    @GET
    @Path("/data")
    @Produces("text/plain")
    //@Produces("image/png")
    //@Produces("application/pdf")
    public Response getCustomerDataFile() {
        
        File file = new File(path);
        
        ResponseBuilder rb = Response.ok((Object) file);
        rb.header("Content-Disposition","attachment; filename=java4sFileFromServer.txt");
        return rb.build();
    }
}

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
Sivateja Kandula - Java/J2EE Full Stack 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

6 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

  6. Sowjanya says:

    Hi SivaTeja,

    line Numbers which are mentioned are missed,
    example : @Produces(โ€œtext/plainโ€) [which i did in line number 24], but its 22.

Name*
Mail*
Website



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

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