Upload File Using RESTful Web Services with JAX-RS

By Arvind Rai, November 21, 2013
On this page we will provide how to upload file using RESTful Web Services with JAX-RS using Jersey. Uploading file using Using RESTful Web Services with JAX-RS is easy. We need to create a method in our resource class. The upload method should be annotated with @POST, @Path and @Consumes. @Consumes take the arguments MediaType. MediaType will be MediaType.MULTIPART_FORM_DATA to upload the file. The upload method will take parameters of @FormDataParam. @FormDataParam provides input to upload file. We need two parameter as InputStream and FormDataContentDisposition. The upload method will save the InputStream and returns Response Object with the file upload status. Find the files for the demo.
package com.concretepage.rest;
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.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
public class FileResource {
	public Response uploadFile(@FormDataParam("upload") InputStream is, 
	                    @FormDataParam("upload") FormDataContentDisposition formData) {
		String fileLocation = "c:/temp/" + formData.getFileName();
		try {
			saveFile(is, fileLocation);
			String result = "Successfully File Uploaded on the path "+fileLocation;
			return Response.status(Status.OK).entity(result).build();
		} catch (IOException e) {
			return Response.status(Status.INTERNAL_SERVER_ERROR).build();
	private void saveFile(InputStream is, String fileLocation) throws IOException {
	    	OutputStream os = new FileOutputStream(new File(fileLocation));
		byte[] buffer = new byte[256];
		int bytes = 0;
		while ((bytes = is.read(buffer)) != -1) {
		     os.write(buffer, 0, bytes);
Create a jsp file for UI to upload the file. Use enctype="multipart/form-data" in form tag. Use input type file to get browse button.
<title>Upload File Demo</title>
	 Upload File Demo<br/><br/>
	<form action="resource/restwb/upload" method="post" enctype="multipart/form-data">
 		 File : <input type="file" name="upload" size="50" />
 	   <input type="submit" value="Upload" />
Configure web.xml to upload file with RESTful web service . Servlet will be com.sun.jersey.spi.container.servlet.ServletContainer and pass the parameter of resource containing package.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	<display-name>JAX RS Application</display-name>
	  <servlet-name>REST Service</servlet-name>
	  <servlet-name>REST Service</servlet-name>
pom.xml for Jar Dependencies of Jersey

Eclipse File Structure

Upload File Using RESTful Web Services with JAX-RS

UI will look like below when we run the Example.

Upload File Using RESTful Web Services with JAX-RS
Download Source Code

©2023 concretepage.com | Privacy Policy | Contact Us