Jax-rs file upload example

RESTful web services:

REST stands for REpresentational State Transfer. Unlike SOAP it is a web standards based architecture and not protocol. It uses HTTP protocol for data communication. REST provides the facility to represent a resource in various formats like text, JSON and XML.

Note: JSON is the most popular format.

Jax-rs file upload jersey example

uploadFile.java

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 com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
 
 
@Path("/file")
public class UploadFileService {
 @POST
 @Path("/upload")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 public Response uploadFile(
  @FormDataParam("file") InputStream uploadedInputStream,
  @FormDataParam("file") FormDataContentDisposition fileDetail) {
 
  String uploadedFileLocation = "D://" + fileDetail.getFileName();
  writeToFile(uploadedInputStream, uploadedFileLocation);
  String output = "File uploaded to : " + uploadedFileLocation;
  return Response.status(200).entity(output).build();
 }
 
 private void writeToFile(InputStream uploadedInputStream,
  String uploadedFileLocation) {
 
  try {
	OutputStream out = new FileOutputStream(new File(
			uploadedFileLocation));
	int read = 0;
	byte[] bytes = new byte[1024];
 
	out = new FileOutputStream(new File(uploadedFileLocation));
	while ((read = uploadedInputStream.read(bytes)) != -1) {
		out.write(bytes, 0, read);
	}
	out.flush();
	out.close();
  } catch (IOException e) {
	e.printStackTrace();
  }
 }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <servlet>
  	<display-name>JAX-RS REST Servlet</display-name>
  	<servlet-name>JAX-RS REST Servlet</servlet-name>
  	<servlet-class>
  	 com.sun.jersey.spi.container.servlet.ServletContainer
  	</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>JAX-RS REST Servlet</servlet-name>
  	<url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<h1>File Upload example with Jersey</h1>
 
<form action="services/file/upload" 
	method="post" enctype="multipart/form-data">
	Select a file: <input type="file" name="file" size="50" />
    <br/><br/>
   <input type="submit" value="Upload" />
</form>
 
</body>
</html>

Output:

jax-rs example7.1
 
Select file
jax-rs example7.2
 
Click on “Upload” button.
jax-rs example7.3
 
Download this example.