728x90 AdSpace

Sunday, 1 December 2013

Download Excel file using JAX-RS RESTful Webservice

In this tutorial we are going to see how to download text file using RESTful web service
@Produces("application/vnd.ms-excel") annotation
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder
add jar
4. Create package called com.javatutorialscorner.jaxrs.downloadfile under RESTful-WebService
5. Create Java class DownloadExcelFile under com.javatutorialscorner.jaxrs.downloadfile package
package com.javatutorialscorner.jaxrs.downloadfile;

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;

public class DownloadExcelFile {

private static final String FILE_PATH = "C://jtc//java-tutorials.xls";
public Response downloadFile() {
File file = new File(FILE_PATH);
ResponseBuilder responseBuilder = Response.ok((Object)file);
"attachment; filename=\"java-tutorials-corner.xls\"");
return responseBuilder.build();

@Produces("application/vnd.ms-excel") used to set output response xls file.
Content-Disposition in response header to tell user agent to pop up download box to download file

6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">


The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL

Now you can run the service and access the service by calling the following URL


Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Download Excel file using JAX-RS RESTful Webservice Rating: 5 Reviewed By: Annamalai Thangaraj