Jersey file upload example – jersey 2 MultiPartFeature

In this Jersey 2 file upload example, we will be learning to upload binary files (e.g. PDF files in this example) using Jersey’s multipart form data support. We will learn below required changes to complete the functionality.

Table of Contents

1. Jersey maven multipart dependency
2. Add MultiPartFeature in web.xml
3. Write Jersey Upload REST API
4. Test file upload using HTML Form
5. Test file upload using jersey client

1. Jersey maven multipart dependency

To use multipart features you need to add jersey-media-multipart module to your pom.xml file:


2. Add Jersey MultiPartFeature in web.xml

Further, you are required to add MultiPartFeature in Jersey configuration to let it know that you will use multipart requests. Simplest way is to add support through web.xml file.


3. Write Jersey File Upload REST API

Now look at actual REST API for file upload, which will receive and save the file.

public Response uploadPdfFile(	@FormDataParam("file") InputStream fileInputStream,
        						@FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
	String UPLOAD_PATH = "c:/temp/";
		int read = 0;
		byte[] bytes = new byte[1024];

		OutputStream out = new FileOutputStream(new File(UPLOAD_PATH + fileMetaData.getFileName()));
		while ((read = != -1) 
			out.write(bytes, 0, read);
	} catch (IOException e) 
		throw new WebApplicationException("Error while uploading file. Please try again !!");
	return Response.ok("Data uploaded successfully !!").build();

5. Test file upload using HTML Form

Simply create a file ‘fileUpload.html‘ file in ‘webapp‘ folder and paste this code.

		<h1>File Upload Example -</h1>
		<form action="rest/upload/pdf" method="post" enctype="multipart/form-data">
			<p>Select a file : <input type="file" name="file" size="45" accept=".pdf" /></p>
			<input type="submit" value="Upload PDF" />

Now hit the URL : “http://localhost:8080/JerseyDemos/fileUpload.html” and it will display a HTML file control to browse the file. Select any PDF file and click on “Upload PDF” button.

Your file will be uploaded and you will get the message: “Data uploaded successfully !!”

5. Test file upload using jersey client

If you are looking for uploading files using java clients then you can modify below working code as per your need.

Jersey file upload using FormDataMultiPart example.

public static void main(String[] args) throws IOException 
	final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();

	final FileDataBodyPart filePart = new FileDataBodyPart("file", new File("C:/temp/sample.pdf"));
	FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
	final FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("foo", "bar").bodyPart(filePart);
	final WebTarget target ="http://localhost:8080/JerseyDemos/rest/upload/pdf");
	final Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType()));
	//Use response object to verify upload success

Drop me your questions in comments box related to jersey 2 multipart file upload example.

Happy Learning !!

Reference :


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.