| 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
- pom.xml & web.xml [ Refer this Restful Hello world example, i am using the same xml’s ]
- RestServiceFileDownloadJava4s.java
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 .:: | ||
![]() | ||
Comments
6 Responses to “Download Files from (JAX-RS) RESTful Web Service”


Thanks sir.your articles are so much informative.Thanks
Can you provide how can i download multiple files and its meta data
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.
Hi Sir,
This tutorial is good,But i need the SOAP Web Service please…Help Me.
so valuable info
Hi SivaTeja,
line Numbers which are mentioned are missed,
example : @Produces(โtext/plainโ) [which i did in line number 24], but its 22.