Download excel file from JAX-RS

In JAX-RS, for excel file, annotate the method with @Produces("application/vnd.ms-excel") :

  1. Put @Produces(“application/vnd.ms-excel”) on service method.
  2. Set “Content-Disposition” in Response header to prompt a download box.

1. Download Excel file in JAX-RS

Full example to download an excel file from JAX-RS.

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("/excel")
public class ExcelService {
 
	private static final String FILE_PATH = "c:\\excel-file.xls";
 
	@GET
	@Path("/get")
	@Produces("application/vnd.ms-excel")
	public Response getFile() {
 
		File file = new File(FILE_PATH);
 
		ResponseBuilder response = Response.ok((Object) file);
		response.header("Content-Disposition",
			"attachment; filename=new-excel-file.xls");
		return response.build();
 
	}
 
}

2. Demo

Access this URI pattern : “/excel/get“.

Figure : Excel file “c:\\excel-file.xls” from server is prompted for user to download, with a new file name “new-excel-file.xls

download excel file from server

Download Source Code

References

  1. JAX-RS @Produces JavaDoc
  2. list of the Application types
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: over here()

  • Pingback: here()

  • Pingback: http://webkingz.camkingz.com/()

  • Pingback: http://makemoney.camkingz.com/()

  • Pingback: shop car insurance()

  • Pingback: water ionizer loans()

  • Pingback: alkaline water brands()

  • Pingback: HD Coloring Pages()

  • Pingback: house blue()

  • Pingback: zak?ad szklarski piotrk√≥w trybunalski()

  • Pingback: pay day loans()

  • Pingback: paypal loans()

  • Pingback: plumbers wrench()

  • Pingback: liquid plumber hair clog()

  • Pingback: locksmith vallejo()

  • Pingback: c e locksmith fayetteville ar()

  • Pingback: online electrician school()

  • Pingback: electrician online games()

  • Pingback: water ionizer plans()

  • Pingback: water ionizer()

  • Pingback: water ionizer machine()

  • Pingback: stop parking()

  • Pingback: bedste laan lige nu()

  • Pingback: mp4 mobile porn()

  • Pingback: parking()

  • Pingback: laan nu og her()

  • Pingback: lan penge online()

  • Pingback: DIRECTV()

  • Pingback: Blue Coaster33()

  • Shiv

    Hey can you please let me know how you can call this rest service from Jquery/Javascript. I am doing an AJAX call but the data format returned isnt correct and it has a lot of spl characters.
    When I fire the URL directly in the browser, it downloads the Excel file correctly.

    $.ajax({
    url:”../control/limitProfile/exportIntradayTradeList/” + gid
    }

  • http://N/A ram

    I am unable to open the apache poi xls in mac os preview
    please help on this

  • sandeep bhowmik

    sir,
    i want to know how to create a simple soap webservice that return a xml file or a text file, using netbeans.

  • jfrancois

    Hello,

    I would like to know how download an excel file jax-rs with a query jpql

  • Vijay

    Hi,

    Have written a similar Restful service using Jersey Framework to render xls file upon request. However am facing below problem while running the service url on a new IE tab [IE version 8]. We are using single-signon authentication mode. The first request gives us the excel report. However if open a new tab in the same IE window and request the URL, it craps out with below error

    “Internet Explorer cannot download xxxx from server
    Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please tyr again later”

    It works in all the other browsers except IE 8. Tried to add below to clear header, but of no avail. Request you to let know if there is anything we can do to avoid this problem

    ResponseBuilder response = Response.ok(stream);
    response.header(“Content-Disposition”,
    “attachment; filename=new-excel-file.xls”);
    response.header(“Pragma”, “no-cache”);
    response.header(“Cache-Control”, “no-cache”);
    response.header(“Cache-Control”, “max-age=0″);
    response.header(“Expires”, “Thu, 01 Nov 2012 16:00:00 GMT”);

    return response.build();

  • Sergey

    Do you have an example how to call it from java script or better jQuery. I can execute it directly from browser but not from web page.