Wednesday, June 25, 2014

RESTFul Webservice: File Upload

File upload feature of html form works by wrapping the file content inside amultipart 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>
    <body>
        <h1>Upload File with RESTFul WebService</h1>
        <form action="rest/fileupload" method="post" enctype="multipart/form-data">
           <p>
            Choose a file : <input type="file" name="file" />
           </p>
           <input type="submit" value="Upload" />
        </form>
    </body>
    </html>

  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);  
    httppost.setEntity(reqEntity);  
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity resEntity = response.getEntity();



  4. CREATE JAX-RS FILE UPLOAD SERVICE WITH JERSEY

  5. 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;
@Path("/fileupload")
public class UploadFileService {
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    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);
            }
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
TEST YOUR SERVICE
Deploy & Browse to {App_URL}/{File_Upload_html}
Upload A file and check.

No comments:

Post a Comment