Tuesday 22 September 2015

Jersey 2 File download example

In previous post, I explained how to upload a file using Jersey. In this post you are going to learn how to download a file(pdf, xlsx etc.,) using Jersey.

Following step-by-step procedure explains complete application using eclipse.

Step 1: Create a dynamic web project “jersey_download” using eclipse.
File -> New -> Dynamic Web Project

Give the project name as ‘jersey_fileupload’.Press Next. Select the checkbox ‘Generate web.xml deployment descriptor’.


Step 2: Convert ‘jersey_fileupload’ to maven.
Rightclick on the project -> Configure -> Convert to Maven project

Step 3: Update web.xml like below.

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"
 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>jersey_download</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
  <servlet-name>Jersey REST Service</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
   <param-name>jersey.config.server.provider.packages</param-name>
   <param-value>com.jersey_filedownload</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>

 </servlet>
 <servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
</web-app>

Step 4: Add maven dependencies in pom.xml.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>jersey_download</groupId>
 <artifactId>jersey_download</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <build>
  <sourceDirectory>src</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <warSourceDirectory>WebContent</warSourceDirectory>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>
  </plugins>
 </build>

 <dependencies>
  <dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-servlet-core</artifactId>
   <version>2.21</version>
  </dependency>

  <dependency>
   <groupId>org.glassfish.jersey.core</groupId>
   <artifactId>jersey-server</artifactId>
   <version>2.21</version>
  </dependency>

 </dependencies>
</project>


Step 5: Create package “com.jersey_filedownload”. Define FileDownloader class.

package com.jersey_filedownload;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

@Path("/file")
public class FileDownloader {
 @GET
 @Path("/download")
 public Response downloadPdfFile() {
  StreamingOutput fileStream = new StreamingOutput() {
   @Override
   public void write(java.io.OutputStream output) throws IOException,
     WebApplicationException {
    try {
     java.nio.file.Path path = Paths.get("/Users/harikrishna_gurram/Decentralized.png");
     byte[] data = Files.readAllBytes(path);
     output.write(data);
     output.flush();
    } catch (Exception e) {
     throw new WebApplicationException("File Not Found !!");
    }
   }
  };
  return Response
    .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
    .header("content-disposition",
      "attachment; filename = Decentralized.png").build();
 }
}


Complete project structure looks like below.

Run application on server, hit following url to download file.

you will get following kind of output.





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment