Publish Your Article Here

How to upload file using Jersey restful web services?

In this page you will see an example for how to upload a file using Jersey API in restful web services.

In order to implement file upload feature, include jersey-multipart dependency in your pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" 

web.xml file reference:

<web-app id="WebApp_ID" version="2.4"

Here is the HTML upload form:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload File Example</title>
	<h1>Upload File</h1>
    <form action="rest/test/upload" method="post" enctype="multipart/form-data">
    	<p>Select a file : <input type="file" name="file"/></p>
		<input type="submit" value="Upload File" />

Here is the upload service class, we are using @FormDataParam annotation to receive uploaded file and FormDataContentDisposition to receive file properties like name, header, etc.

package com.java2novice.restful;


import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

public class RestUploadService {

	private static final String FOLDER_PATH = "C:\my_files\";
	public String uploadFile(@FormDataParam("file") InputStream fis,
					@FormDataParam("file") FormDataContentDisposition fdcd) {
		OutputStream outpuStream = null;
		String fileName = fdcd.getFileName();
		System.out.println("File Name: " + fdcd.getFileName());
		String filePath = FOLDER_PATH + fileName;
		try {
			int read = 0;
			byte[] bytes = new byte[1024];
			outpuStream = new FileOutputStream(new File(filePath));
			while ((read = != -1) {
				outpuStream.write(bytes, 0, read);
		} catch(IOException iox){
		} finally {
			if(outpuStream != null){
				try{outpuStream.close();} catch(Exception ex){}
		return "File Upload Successfully !!";

Try below URL to upload file:


User Upload Form


<< Previous Program | Next Program >>

Restful Web Services Examples

  1. Restful web services using RESTEasy hello world example.
  2. Restful web services using Jersey hello world example.
  3. JAX-RS @Path annotation example
  4. JAX-RS @Path annotation with regular expression match example
  5. JAX-RS @PathParam annotation example
  6. JAX-RS @QueryParam and @DefaultValue annotations example
  7. JAX-RS @MatrixParam annotation example
  8. How to read multiple values of a query paramter in JAX-RS restful web services?
  9. How to pass header parameters as method inputs in JAX-RS restful web services?
  10. How to read header parameters in JAX-RS restful web services?
  11. JAX-RS @FormParam annotation example
  12. How to upload file using Jersey restful web services?
  13. How to download file using java restful web services?
  14. XML based Restful web service with RESTEasy and JAXB.
  15. XML based Restful web service with Jersey and JAXB.
  16. Json based Restful web service with RESTEasy, Jettison and JAXB
  17. Json based Restful web service with RESTEasy and Jackson
  18. Json based Restful web service with Jersey and Jackson
  19. How to input json request with Jersey and Jackson?
  20. Java client for restful web service using package
  21. Java client for restful web service using Jersey API
  22. Java restful webservices with HTTP basic authentication.
Knowledge Centre
What is wrapper class?
Everything in java is an object, except primitives. Primitives are int, short, long, boolean, etc. Since they are not objects, they cannot return as objects, and collection of objects. To support this, java provides wrapper classes to move primitives to objects. Some of the wrapper classes are Integer, Long, Boolean, etc.
Famous Quotations
The greatest obstacle to discovery is not ignorance; it is the illusion of knowledge.
-- Daniel J. Boorstin

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2018 by Nataraja Gootooru. All Rights Reserved.