RESTEasy – File Download Example

RESTEasy is JBOSS provided implementation of JAX-RS specification for building RESTful Web Services and RESTful Java applications. RESTEasy works in combination with HTTP media types for providing the response in specific formats such as images, pdf or text.

The core component to configure for enabling support for multiple media types in response is @Produces annotation. A full list of such media types can be found in this link.

For demonstration purpose, I will be showing example of downloading one image, one text and one pdf file. Similarly you can build for other media types (file types).

1) Create a maven project

mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication 
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

mvn eclipse:eclipse -Dwtpversion=2.0

2) Update jax-rs dependencies in pom.xml file

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.howtodoinjava</groupId>
  <artifactId>RESTfulDemoApplication</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>RESTfulDemoApplication Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <repositories>
   	<repository>
      <id>jboss</id>
      <url>http://repository.jboss.org/maven2</url>
   	</repository>
  </repositories>
  <dependencies>
    <!-- core library -->
	<dependency>
		<groupId>org.jboss.resteasy</groupId>
		 <artifactId>resteasy-jaxrs</artifactId>
		<version>2.3.1.GA</version>
	</dependency>
	<dependency>
		<groupId>net.sf.scannotation</groupId>
		<artifactId>scannotation</artifactId>
		<version>1.0.2</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>RESTfulDemoApplication</finalName>
  </build>
</project>

3) Create service class and APIs with corresponding media types in @Produces annotation

package com.howtodoinjava.service;

import java.io.File;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;

@Path("/file-management")
public class FileServer
{
	@GET
	@Path("/{fileName}/text")
	@Produces("text/plain")
	public Response getFileInTextFormat(@PathParam("fileName") String fileName) 
	{
		System.out.println("File requested is : " + fileName);
		
		//Put some validations here such as invalid file name or missing file name
		if(fileName == null || fileName.isEmpty())
		{
			ResponseBuilder response = Response.status(Status.BAD_REQUEST);
			return response.build();
		}
		
		//Prepare a file object with file to return
		File file = new File("c:/demoTxtFile.txt");
		
		ResponseBuilder response = Response.ok((Object) file);
		response.header("Content-Disposition", "attachment; filename="howtodoinjava.txt"");
		return response.build();
	}
	
	@GET
	@Path("/{fileName}/pdf")
	@Produces("application/pdf")
	public Response getFileInPDFFormat(@PathParam("fileName") String fileName) 
	{
		System.out.println("File requested is : " + fileName);
		
		//Put some validations here such as invalid file name or missing file name
		if(fileName == null || fileName.isEmpty())
		{
			ResponseBuilder response = Response.status(Status.BAD_REQUEST);
			return response.build();
		}
		
		//Prepare a file object with file to return
		File file = new File("c:/demoPDFFile.pdf");
		
		ResponseBuilder response = Response.ok((Object) file);
		response.header("Content-Disposition", "attachment; filename="howtodoinjava.pdf"");
		return response.build();
	}
	
	@GET
	@Path("/{fileName}/image")
	@Produces("image/jpeg")
	public Response getFileInJPEGFormat(@PathParam("fileName") String fileName) 
	{
		System.out.println("File requested is : " + fileName);
		
		//Put some validations here such as invalid file name or missing file name
		if(fileName == null || fileName.isEmpty())
		{
			ResponseBuilder response = Response.status(Status.BAD_REQUEST);
			return response.build();
		}
		
		//Prepare a file object with file to return
		File file = new File("c:/demoJpegFile.jpeg");
		
		ResponseBuilder response = Response.ok((Object) file);
		response.header("Content-Disposition", "attachment; filename="howtodoinjava.jpeg"");
		return response.build();
	}
}

4) Put files to be download in referenced locations

I am referencing files from “c:/<file>”. Copy files from distribution folder in download link, and paste them in c: before running this example.

5) Test the application

Below are snaps of download window appear in browser for requesting URLs:

http://localhost:8080/RESTfulDemoApplication/file-management/demoTxtFile/text

download_txt

http://localhost:8080/RESTfulDemoApplication/file-management/demoPDFFile/pdf

download_pdf
http://localhost:8080/RESTfulDemoApplication/file-management/demoJpegFile/image

download_image

 

If you want to download sourcecode of above example, follow below given link.

Source code download

Happy Learning !!

Comments

Subscribe
Notify of
guest
10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode