Saturday 12, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 30 November 2013

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
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project as given below.
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 DownloadTextFile under com.javatutorialscorner.jaxrs.downloadfile package
DownloadTextFile.java
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;

@Path("/filedownload")
public class DownloadTextFile {

private static final String FILE_PATH = "C://jtc//java-tutorials.txt";
@GET
@Path("/txt-file")
@Produces("text/plain")
public Response downloadFile() {
File file = new File(FILE_PATH);
ResponseBuilder responseBuilder = Response.ok((Object)file);
responseBuilder.header("Content-Disposition",
"attachment; filename=\"java-tutorials-corner.txt\"");
return responseBuilder.build();
}
}

@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

6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.
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">
<display-name>JAX-RS-Path</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.javatutorialscorner.jaxrs.downloadfile</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

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

http://localhost:8080/RESTful-WebService/rest/filedownload/txt-file

Output
output

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • JAX-RS @PATH for URIIn previous tutorials we have seen simple Hello World RESTful Web service using JAX-RS.In this tutorial we will we see @PATH annotation used for URI in RESTful web service.follow the steps given belowCreate new Dynamic Web Pr… Read More
  • Hello World RESTful Web serviceCreate new Dynamic Web Project and  give project name as RESTful_HelloWorld Copy all jar files into WEB-INF/lib folder.Select build path add jar in your build path.Create ClassCreate package called com.javatutorialscorn… Read More
  • REST - Representational State TransferREST-IntroductionREST is short form of Representational State TransferRest is architectural style web service which is based on web standards and http protocols(get,post,put,delete).REST was invented by Roy Fielding in 2000.I… Read More
  • JAX-RS QueryParamIn this tutorial we are going to see how to pass parameters to RESTful web service via URL using @QueryParam1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called… Read More
  • JAX-RS Multiple Parameter with @PathParamIn this tutorial we are going to see how to pass multiple parameters to RESTful web service via URL using @PathParam 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Proje… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Download Text file using JAX-RS RESTful Webservice Rating: 5 Reviewed By: eHowToNow