Download Text file using JAX-RS RESTful Webservice

In this tutorial we are going to see how to download text file using RESTful web service @Produces(“text/plain”) annotation.

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 2.x maven dependency configuration below. If you are new to maven refer our maven tutorials.

Jersey 2.x dependency in pom.xml

<project xmlns="" xmlns:xsi=""
  <name>RestTutorials Maven Webapp</name>
      <!-- JAX-RS -->
        <!-- Jersey 2.19 -->

package com.ehowtonow.webservice;



public class DownloadTextFile {

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

@Produces(“text/plain”) used to set output response text file. Content-Disposition in response header to tell user agent to pop up download box to download file

Configure Jersey 2.x Servlet dispatcher

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

	<display-name>Restful Webservice Example</display-name>


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



Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Restful Webservice HelloWorld Jersey 2.x Example
  2. JAX-RS Send List using QueryParam Annotation
  3. JAX-RS @FormParam Web service example
  4. RESTful Web service to consume JSON and Produce XML – Jersey 1.x
  5. JAX-RS @GET RESTful webservice
  6. JAX-RS @POST using RESTful web service – Jersey 1.x
  7. JAX-RS @MatrixParam Restful Webservice
  8. JAX-RS Get HTTP Headers using @Context
  9. JAX-RS @PATH for URI – Jersey
  10. Restful Webservice HelloWorld Example Jersey 1.x
  11. JAX-RS Web Service to produce XML using JAXB
  12. JAX-RS Web service to Consume JSON using Jersey 1.x
  13. Download Excel file using JAX-RS RESTful Webservice
  14. File Upload using JAX-RS RESTful Webservice (Jersey 2.x)
  15. JAX-RS Get HTTP Headers using @HeaderParam

Be the first to comment

Leave a Reply