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).
Prerequisites
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.
maven-arctype-webapp
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.roytuts</groupId>
<artifactId>rest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>rest Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<jersey.version>2.23.2</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<build>
<finalName>rest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project> 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
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>REST Service</display-name> <servlet> <servlet-name>REST</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.roytuts.rest.resources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map /rest/* to Jersey --> <servlet-mapping> <servlet-name>REST</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
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;
@Path("/file")
public class RestService {
@POST
@Path("/upload")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@Produces(MediaType.TEXT_PLAIN)
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) {
e.printStackTrace();
} finally {
try {
out.close();
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return Response.status(Response.Status.OK).entity("File '" + filePath + "' uploaded successfully")
.type(MediaType.TEXT_PLAIN).build();
}
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));
System.out.println(response.readEntity(String.class));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
} 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

Very good tutorial!!! Thanks!!!
thanks