Upload file using REST webservice

In this tutorial I am going to show you how we can upload file using REST or RESTful web service.

The most important concept in REST is resources, which are identified by global IDs — typically using URIs. Client applications use HTTP methods (GET/ POST/ PUT/ DELETE) to manipulate the resource or collection of resources. A RESTful Web service is implemented using HTTP and the principles of REST. Typically, a RESTful Web service should define the following aspects:

The base/root URI for the Web service such as http://<host>/<appcontext/contextpath>/<url pattern>/<resources>.
The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.
The set of operations supported by the service. (for example, POST, GET, PUT or DELETE).

Eclipse, JDK 1.8, JAX-RS jars 2.23.2
Have maven installed and configured
JAX-RS dependencies in pom.xml

For this tutorial we will create a web maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Step 1. Create a web maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : rest
Step 2. Modify the pom.xml file as shown below.

<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">
	<name>rest Maven Webapp</name>

Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Modify web.xml file to use the jersey servlet

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
	<display-name>REST Service</display-name>
	<!-- Map /rest/* to Jersey -->

Step 5. Create a REST resource class as shown below

package com.roytuts.rest.resources;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
public class RestService {
	public Response sendFile(@Context HttpHeaders headers, InputStream fileInputStream) {
		MultivaluedMap<String, String> map = headers.getRequestHeaders();
		String fileName = getFileName(map);
		OutputStream out = null;
		String filePath = "D:/" + fileName;
		try {
			out = new FileOutputStream(new File(filePath));
			byte[] buf = new byte[1024];
			int len;
			while ((len = fileInputStream.read(buf)) > 0) {
				out.write(buf, 0, len);
		} catch (Exception e) {
		} finally {
			try {
			} catch (IOException e) {
		return Response.status(Response.Status.OK).entity("File '" + filePath + "' uploaded successfully")
	private String getFileName(MultivaluedMap<String, String> headers) {
		String[] contentDisposition = headers.getFirst("Content-Disposition").split(";");
		for (String filename : contentDisposition) {
			if ((filename.trim().startsWith("filename"))) {
				String[] name = filename.split("=");
				String finalFileName = name[1].trim().replaceAll(""", "");
				return finalFileName;
		return "";

In the above class, the file will be uploaded to the directory D:

Step 6. Create a main class which will be used for testing the file upload fucntionality

package com.roytuts.rest.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class RestTest {
	public static void main(String[] args) {
		try {
			File file = new File("D:/Documents/JavaJ2EE.doc");
			InputStream fileInStream = new FileInputStream(file);
			String contentDisposition = "attachment; filename="" + file.getName() + """;
			Client client = ClientBuilder.newClient();
			Response response = client.target("http://localhost:8080/rest/file").path("upload").request()
					.header("Content-Disposition", contentDisposition)
					.post(Entity.entity(fileInStream, MediaType.APPLICATION_OCTET_STREAM));
		} catch (FileNotFoundException e) {

Step 7. Now run the application on Tomcat 8. Run the above main class. Make sure you have a file JavaJ2EE.doc under D:Documents directory because this file will be uploaded using the REST service. Once the file is uploaded to D: drive then you will see the following output

REST file upload
Thanks for reading.

2 thoughts on “Upload file using REST webservice

Leave a Reply

Your email address will not be published. Required fields are marked *