728x90 AdSpace

Sunday, 1 December 2013

File Upload using JAX-RS RESTful Webservice

In this tutorial we are going to see how to upload file using Jersey RESTful web service
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder
and add mimepull-1.4.jar also to upload multi part data
4.Create file-upload.jsp under WebContent folder.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<form action="rest/fileupload/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload">

5. Create package called com.javatutorialscorner.jaxrs.uploadfile under RESTful-WebService
6. Create Java class FileUploadService under com.javatutorialscorner.jaxrs.uploadfile package

package com.javatutorialscorner.jaxrs.uploadfile;

import java.io.File;
import java.io.FileOutputStream;
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 com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

public class FileUploadService {

String response = "";

public Response uploadFile(@FormDataParam("file") InputStream inputStream,
@FormDataParam("file") FormDataContentDisposition file) {

try {
final String FILE_DESTINATION = "C://jtc//" + file.getFileName();
File f = new File(FILE_DESTINATION);
OutputStream outputStream = new FileOutputStream(f);
int size = 0;
byte[] bytes = new byte[1024];
while ((size = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, size);

response = "File uploaded " + FILE_DESTINATION;
} catch (Exception e) {

return Response.status(200).entity(response).build();

7.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">


The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL
Now you can run file-upload.jsp by calling following URL


upload file to



file upload after file uploaded response from server

Annamalai Thangaraj

Annamalai is a Software Engineer with 2+ years experience in Java, Spring, Struts, Hibernate, IDM/IAM, and Enterprise Web Application Development.

Website: Java Tutorials Corner

  • Blogger Comments
  • Disqus Comments


Post a Comment

Item Reviewed: File Upload using JAX-RS RESTful Webservice Rating: 5 Reviewed By: Annamalai Thangaraj
Scroll to Top