Jersey (JAX-RS) single file upload example

Jersey provide easy mechanism to let client upload file on server. In this tutorial we will learn single file upload to Jersey (JAX-RS) endpoint.

This is a part of Jersey (JAX-RS) Restful Web Services Development Guides series. Please read Jersey + Spring Boot getting started guide.

1. Include Jersey media multipart dependency in Gradle

File: build.gradle (snippet)

dependencies {
     compile 'org.springframework.boot:spring-boot-starter-web',
    testCompile 'org.springframework.boot:spring-boot-starter-test'

Jersey provide multipart form data support using separate extension. We need to include that library as a dependency in application. Include '' as compile time dependency.

2. HTML form with single file input

File: /src/main/resources/static/file-upload.html

<!DOCTYPE html>
<meta charset="UTF-8">
<title>Upload File Test</title>
	<h2>Upload file</h2>
	<form action="/upload/file" enctype="multipart/form-data" method="post">
		<label>Select File</label><input type= "file" name="file" /> <br/><br/>
		<label>Tags</label> <input name="tags" maxlength="10"/> <br/><br/>
		<input type="submit" title="Save"/>

A HTML form with multipart/form-data support with single file selection.

3. Use @FormDataParam injection

We can inject named multipart form values (File, binary, text etc.) using @FormDataParam, For e.g. form elements with name file and tags.

File: /src/main/java/in/geekmj/resource/

package in.geekmj.resource;

import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.List;


import org.springframework.stereotype.Component;

 * @author geekmj Three ways to get Form data in Jersey

public class FileUploadResource {

	public Response uploadFile(@DefaultValue("") @FormDataParam("tags") String tags, 
				@FormDataParam("file") InputStream file,
				@FormDataParam("file") FormDataContentDisposition fileDisposition) {

		String fileName = fileDisposition.getFileName();
		saveFile(file, fileName);
		String fileDetails = "File saved at /Volumes/Drive2/temp/file/" + fileName + " with tags "+ tags;


		return Response.ok(fileDetails).build();
	private void saveFile(InputStream file, String name) {
		try {
			/* Change directory path */
			java.nio.file.Path path = FileSystems.getDefault().getPath("/Volumes/Drive2/temp/file/" + name); 
			/* Save InputStream as file */
			Files.copy(file, path);
		} catch (IOException ie) {


Using @FormDataParam we have injected input type file as InputStream and its FileDataContentDisposition. We have injected a text input field tags also.

4. Test single file upload

Download complete source code for this project and then follow this instruction to run the application.

Go to http://localhost:8080/file-upload.html

Single file upload to jersey endpoint

Single file upload to jersey endpoint demo


  1. Official Jersey Documentation
  2. W3C multipartform/form-data documentation
  3. Jersey multipartform support extension maven repository
  4. InputStream
Download Icon SmallDownload the Full ProjectGithub IconFollow Project On Github

Leave a Reply

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