Uploading a File with RESTEasy and HttpClient

In previous posts, we learned about file downloading and building RESTful clients. Now, let move further. In this post, I am giving sample code of file upload using jax-rs resteasy. For uploading the file, httpclient library will be used instead of HTML form.

I am using MultipartFormDataInput class which is part of resteasy-multipart plugin.

1) Update project’s maven dependencies

Add/Update below maven dependencies to add support for multipart file upload in you project.

<!-- core library -->
	<!-- JAXB provider -->
	<!-- Multipart support -->
	<!-- For better I/O control -->

Also add jar files given in below picture. They are needed to build client code for file upload example.

HTTP client jar files
HTTP client jar files


2) Prepare the http client which will upload the file on client side

package com.howtodoinjava.client.upload;
import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
public class DemoFileUploader 
	public static void main(String args[]) throws Exception
    	DemoFileUploader fileUpload = new DemoFileUploader () ;
    	File file = new File("C:/Lokesh/Setup/workspace/RESTfulDemoApplication/target/classes/Tulips.jpg") ;
    	//Upload the file
        		file, file.getName(), "File Uploaded :: Tulips.jpg") ;
    public void executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription) throws Exception 
    	HttpClient client = new DefaultHttpClient() ;
        HttpPost postRequest = new HttpPost (urlString) ;
        	//Set various attributes 
            MultipartEntity multiPartEntity = new MultipartEntity () ;
            multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : "")) ;
            multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())) ;
            FileBody fileBody = new FileBody(file, "application/octect-stream") ;
            //Prepare payload
            multiPartEntity.addPart("attachment", fileBody) ;
            //Set to request body
            postRequest.setEntity(multiPartEntity) ;
            //Send request
            HttpResponse response = client.execute(postRequest) ;
            //Verify response if any
            if (response != null)
        catch (Exception ex)
            ex.printStackTrace() ;

3) Write RESTful API in application to accept multipart request

package com.howtodoinjava.client.upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;

public class DemoFileSaver_MultipartFormDataInput 
	private final String UPLOADED_FILE_PATH = "c:\temp\";

	public Response uploadFile(MultipartFormDataInput input) throws IOException 
		//Get API input data
		Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
		//Get file name
		String fileName = uploadForm.get("fileName").get(0).getBodyAsString();
		//Get file data to save
		List<InputPart> inputParts = uploadForm.get("attachment");

		for (InputPart inputPart : inputParts)
				//Use this header for extra processing if required
				MultivaluedMap<String, String> header = inputPart.getHeaders();
				// convert the uploaded file to inputstream
				InputStream inputStream = inputPart.getBody(InputStream.class, null);
				byte[] bytes = IOUtils.toByteArray(inputStream);
				// constructs upload file path
				fileName = UPLOADED_FILE_PATH + fileName;
				writeFile(bytes, fileName);
				System.out.println("Success !!!!!");
			catch (Exception e) 
		return Response.status(200)
				.entity("Uploaded file name : "+ fileName).build();

	//Utility method
	private void writeFile(byte[] content, String filename) throws IOException 
		File file = new File(filename);
		if (!file.exists()) {
		FileOutputStream fop = new FileOutputStream(file);

Sourcecode download

Happy Learning !!


Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.