Table of contents
1.
Introduction
2.
Specifying Content Type
3.
JAX RS Text File Download
3.1.
Project Structure
3.2.
Code
3.2.1.
Java File Code
3.2.2.
XML File Code
3.2.3.
HTML File Code
3.3.
Output
4.
Frequently Asked Questions
4.1.
What makes API and REST API different?
4.2.
Are JAX-RS and Jersey the same?
4.3.
Which annotation specifies the type of file content in JAX RS?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

JAX-RS File Download Example

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JAX-RS API allows us to download text, pdf, picture, and excel files in Java. We only need a few lines of code to accomplish this. For building JAX-RS file download examples, we are utilising the jersey implementation.

JAX-RS File Download Example

Specifying Content Type

To download several types of files, the user must pick the different content types. The annotation "@Produces" specifies the nature of file content.

  1. @Produces("text/plain"): This gets used for downloading a text file.
  2. @Produces("application/pdf"): This gets used for downloading PDF file.
  3. @Produces("application/msword"): This gets used for downloading ms word files.
  4. @Produces("application/vnd.ms-excel"): This gets used for downloading excel file.
  5. @Produces("image/png"): This gets used for downloading a png image file.

JAX RS Text File Download

Below we will see an example of JAX RS Text File Download. We will look at the project structure and the files to see how the codes have been configured.

Project Structure

The project structure of our JAX RS File download project is an essential element. The one shown below is from Eclipse integrated development environment. There are three files: The Java File, The XML File and the HTML file. They all have the essential codes to run our example.

The Project Structure for out JAX RS  File download Project

Code

We can see the codes below for the download example. Which has been divided into a three-part structure.

Java File Code

    package com.codingNinjas.rest;  
    import javax.ws.rs.Produces;
    import javax.ws.rs.GET;  
    import java.io.File; 
    import javax.ws.rs.Path;  
      
    import javax.ws.rs.core.Response;  
    import javax.ws.rs.core.Response.ResponseBuilder;  
    @Path("/files")
    public class CodingNinjasDownload {  
     //here we add the file path where the target is located
        private static final String FILE_PATH = "here_you_add_your_file_directory";  
        @GET  
        @Path("/txt")  
        @Produces("text/plain")  
        public Response getFile() {  
            File file = new File(FILE_PATH);  
       
            ResponseBuilder response = Response.ok((Object) file);  
            response.header("Content-Disposition","attachment; filename=\"CodingNinjas_JaxRS.txt\"");  
            return response.build();  
       
        }  
     }  
You can also try this code with Online Java Compiler
Run Code

XML File Code

<?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">  
     <servlet>  
        <servlet-name>Coding Ninjas Studio-REST-servlet</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.Coding Ninjas Studio.rest</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
      </servlet>  
      <servlet-mapping>  
        <servlet-name>Coding Ninjas Studio-REST-servlet</servlet-name>  
        <url-pattern>/rest/*</url-pattern>  
      </servlet-mapping>  
    </web-app>

HTML File Code

   <a href="rest/files/txt">Download Coding Ninjas Text File</a> 

Output

As we run the code, we will see the server is successfully running in the terminal. Following this, we will see the download prompt in our server/browser.

Successfully Running Terminal Photo

Below we can have a look at the download prompt, which can be seen in the browser view. The available option, when pressed, will download the intended text file present at the source.

Browser or Server Photo

Frequently Asked Questions

What makes API and REST API different?

API's main objective is to standardise data transmission across online services. The protocol used varies depending on the type of API. REST API, on the other hand, is an architectural style for developing online services that communicate via the HTTP protocol.

Are JAX-RS and Jersey the same?

Jersey is a JAX-RS implementation, while JAX-RS is a specification. Jersey is more than just a JAX-RS Reference Implementation. Jersey has its own API, which extends the JAX-RS toolkit with additional capabilities and utilities to make RESTful service and client creation even easier.

Which annotation specifies the type of file content in JAX RS?

The @Produces annotation in JAX RS gets used to define the type of file content

Conclusion

In the article, we found out about JAX-RS File download examples. We also saw the different types of content types that we have to have and the annotations. We also implemented a real-life code and saw its output to get a real-time example. 

Recommended Readings:

Refer to our courses and explore Coding Ninjas Studio to find more exciting stuff. You can also look into the interview experiences and solve different problems. Look into our Guided paths, test series, libraries and resources to know more.

Thank You

Happy Coding!

Live masterclass