Upload File Using RESTful Web Services with JAX-RS

By Arvind Rai, November 21, 2013
On this page we will provide how to upload file using RESTful Web Services with JAX-RS using Jersey. Uploading file using Using RESTful Web Services with JAX-RS is easy. We need to create a method in our resource class. The upload method should be annotated with @POST, @Path and @Consumes. @Consumes take the arguments MediaType. MediaType will be MediaType.MULTIPART_FORM_DATA to upload the file. The upload method will take parameters of @FormDataParam. @FormDataParam provides input to upload file. We need two parameter as InputStream and FormDataContentDisposition. The upload method will save the InputStream and returns Response Object with the file upload status. Find the files for the demo.
FileResource.java
package com.concretepage.rest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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 javax.ws.rs.core.Response.Status;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
@Path("/restwb") 
public class FileResource {
	@POST
	@Path("/upload") 
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	public Response uploadFile(@FormDataParam("upload") InputStream is, 
	                    @FormDataParam("upload") FormDataContentDisposition formData) {
		String fileLocation = "c:/temp/" + formData.getFileName();
		try {
			saveFile(is, fileLocation);
			String result = "Successfully File Uploaded on the path "+fileLocation;
			return Response.status(Status.OK).entity(result).build();
		} catch (IOException e) {
			e.printStackTrace();
			return Response.status(Status.INTERNAL_SERVER_ERROR).build();
		}
	}
	private void saveFile(InputStream is, String fileLocation) throws IOException {
	    	OutputStream os = new FileOutputStream(new File(fileLocation));
		byte[] buffer = new byte[256];
		int bytes = 0;
		while ((bytes = is.read(buffer)) != -1) {
		     os.write(buffer, 0, bytes);
		}
        }
} 
Create a jsp file for UI to upload the file. Use enctype="multipart/form-data" in form tag. Use input type file to get browse button.
fileUpload.jsp
<html>
<title>Upload File Demo</title>
<body>
	 Upload File Demo<br/><br/>
	<form action="resource/restwb/upload" method="post" enctype="multipart/form-data">
 		 File : <input type="file" name="upload" size="50" />
	  <br/>
 	   <input type="submit" value="Upload" />
	</form>
 </body>
</html> 
Configure web.xml to upload file with RESTful web service . Servlet will be com.sun.jersey.spi.container.servlet.ServletContainer and pass the parameter of resource containing package.
web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
	<display-name>JAX RS Application</display-name>
	<servlet>
	  <servlet-name>REST Service</servlet-name>
	<servlet-class>
	  com.sun.jersey.spi.container.servlet.ServletContainer
	</servlet-class>
	  <init-param>
	    <param-name>com.sun.jersey.config.property.packages</param-name>
	    <param-value>com.concretepage.rest</param-value>
	  </init-param>
	  <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	  <servlet-name>REST Service</servlet-name>
	  <url-pattern>/resource/*</url-pattern>
	</servlet-mapping>
</web-app> 
pom.xml for Jar Dependencies of Jersey
<dependencies>
   <dependency>
	<groupId>javax.ws.rs</groupId>
	<artifactId>jsr311-api</artifactId>
	<version>1.1.1</version>
   </dependency>
   <dependency>
	<groupId>com.sun.jersey</groupId>
	<artifactId>jersey-server</artifactId>
	<version>1.8</version>
   </dependency>
   <dependency>
	<groupId>com.sun.jersey.contribs</groupId>
	<artifactId>jersey-multipart</artifactId>
	<version>1.8</version>
   </dependency>
</dependencies> 

Eclipse File Structure

Upload File Using RESTful Web Services with JAX-RS

UI will look like below when we run the Example.

Upload File Using RESTful Web Services with JAX-RS
Download Source Code
upload-file-using-restful-web-services-jax-rs.zip
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us