, ,

In this tutorial, we will learn how do to file upload with Jersey, A RESTFul Webservice(JAX-RS) implementation.

File upload feature of html form works by wrapping the file content inside a multipart envelope as the content type “application/x-www-form-urlencoded” is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type “multipart/form-data” should be used for submitting forms that contain files, non-ASCII data, and binary data.

  1. Resolve Dependency

    For theĀ “multipart/form-data” support we need to include “jersey-multipart.jar” and dependencies from here [for glassfish v3.1 & upward you will find it in glassfish\modules].

  2.  <html>
        <h1>Upload File with RESTFul WebService</h1>
        <form action="rest/fileupload" method="post" enctype="multipart/form-data">
            Choose a file : <input type="file" name="file" />
           <input type="submit" value="Upload" />
  3. Java File Upload Client

    We will use Apache HTTPClient library to resolve dependency. It is an optional step & the code could be exploited to build a test-client.

     HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    FileBody fileContent= new FileBody(new File(fileName));
    StringBody comment = new StringBody("Filename: " + fileName);
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("file", fileContent);
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();
  4. Create JAX-RS File Upload Service with Jersey

    Jersey usesĀ  @FormDataParam to receive the uploaded file. To get the uploaded file name or header detail, compare with “FormDataContentDisposition” which represents a “Content-Disposition” header whose value is “form-data”.

    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;
    public class UploadFileService {
    	public Response uploadFile(
    		@FormDataParam("file") InputStream uploadedInputStream,
    		@FormDataParam("file") FormDataContentDisposition fileDetail) {
    		String uploadedFileLocation = "c://uploadedFiles/" + fileDetail.getFileName();
    		// save it
    		saveToFile(uploadedInputStream, uploadedFileLocation);
    		String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
    		return Response.status(200).entity(output).build();
    	// save uploaded file to new location
    	private void saveToFile(InputStream uploadedInputStream,
    		String uploadedFileLocation) {
    		try {
    			OutputStream out = null;
    			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);
    		} catch (IOException e) {
  5. Test Your Service

    1. Deploy & Browse to {App_URL}/{File_Upload_html}
    2. Upload A file and check.
  6. Source Code

    You can download source-code here.

About these ads