Download Base64 encoded image via Jersey 2 REST web service


This tutorial demonstrates how to download a Base64 encoded image using a RESTful web service with Jersey 2 and Tomcat 8. The example Java project is available on Git and requires Maven.

1. Prerequisites

  1. Install Apache Tomcat (we use version 8.5). If not already installed, please have a look at these 2 tutorials:
  2. Install Apache Maven to build the project
  3. You can just run a “clean install” on the Maven project and deploy the created WAR file to Tomcat directly. Although we recommend to work with an IDE like EclipseEE or IntelliJ to adapt or debug the project
  4. If you import the Maven project to EclipseEE, activate and adapt the Project Facets to use the Dynamic Web Project 3.1, Java 1.8 and JAX-RS 2.0

2. Code step-by-step

Starting of with the project setup, we explain the web.xml, some dependencies in the pom.xml and move on to the code example.

2.1 Configuration of web.xml

The web.xml is straight forward. We specify a welcome file that is displayed when requesting the root folder of our Servlet. In order to do so you have to use another sub root for your Servlet mapping (like <url-pattern>/rest/*</url-pattern>). Using <url-pattern>/*</url-pattern> leads to undefined behavior since the Servlet and the WebContent share the same root.

Furthermore we define the applications initialization parameters <param-value></param-value>. You can do that via a ResourceConfig file as well, but we only use one REST service in this example.

2.2 Dependencies in pom.xml

Nothing too fancy here either. We use Jersey 2 in version 2.25, the Apache Commons library in version 2.5 as Base64 encoder. Furthermore we specify the Java 1.8 compiler and the WAR file source directory.

2.3 Base64 download REST web service

This is basically our only Java code. We create a download web service that extends the ResourceConfig. Then we provide one GET method that is reachable via …/jersey2-base64-image-download/rest/download/image_name.jpg and produces MediaType.APPLICATION_OCTET_STREAM.

The most interesting part here is the Base64 encoder of the Apache Commons library and how the response is built.

In the response we specify the byte array data and the content type. Afterwards, we enhance the response header for Content-Disposition with the parameter inline. This indicates that the image should be displayed and not downloaded. Using the parameter attachment will result in the file download. Additionally we attach the file name.

2.4 Display the image via index.html

We use JQuery to retrieve the data from the previously presented GET method. You have a button to click on to display the image. Remember to adapt the GET call if you run your server on a different port than the standard 8080. The index.html is placed in the folder WebContent/WEB-INF/ and refers to our welcome file in the web.xml.

3. Build and run the project

Build the project via the Maven command clean install. This should create a jersey2-base64-image-download.war file.

Check out this tutorial chapter 10 for Eclipse users if you need help with the deployment. However, you can deploy directly from Eclipse or IntelliJ or copy the WAR file into the Tomcat WebApps folder. After the successful deployment and with a running Tomcat, you should see the following website when visiting http://localhost:8080/jersey2-base64-image-download/. Remember to adapt different port and mapping settings if you changed anything.


Click the Show Image button in order to download and display the provided image. The image path is hard coded to an example image located in the resources folder (RoadToDataScientist.jpg).

4. Conclusion

This small example project demonstrates that very little code is required to download and display a Base64 encoded image via a Jersey 2 web service. We feel that the most trouble is not actually writing the code, but getting the configuration (especially the web.xml) to work correctly.

If you have problems or errors, feel free to comment and ask.


Leave a Reply