Wednesday, June 25, 2014

JAX-RS Download File Using Jersey API

In this example we are going to see how you can download a file from a JAX-RS REST Service. It's easy to do that, as it requires to annotate the bind method with the @Produces annotation. The @Produces annotation can used to used to specify the MIME media types a service can produce and send to the client. In this example we are going to see how you can download text, image, PDF and excel files.
The second thing to note is that you have to attach the file to the response and then set the Content-Disposition header variable of the response. The Content-Disposition response-header field is used to dictate a a default file name if the user requests that the content is saved to a file. A simple example would be : Content-Disposition: attachment; filename="fname.ext". Using this, the user-agent will not display the response, but directly the pop up "Save as…" dialogue.

To download different file formats all you have to do is change the type registered in @Produces annotation.
  • @Produces("image/png") for images (you can change the "png" part according to the format of the image).
  • @Produces("application/pdf") for PDF files.
  • @Produces("application/vnd.ms-excel") for Excel files.


import java.io.File;


import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.Response;

import javax.ws.rs.core.Response.ResponseBuilder;


@Path("/files")

public class HelloWorldREST {


    // The file paths of the files in the server

    private static final String TXT_FILE = "C:\\test.txt";

    private static final String IMAGE_FILE = "C:\\test.png";

    private static final String PDF_FILE = "C:\\test.pdf";

    private static final String EXCEL_FILE = "C:\\test.xls";


    /**

     *  Download Text File

     */

    @GET

    @Path("/txt")

    @Produces("text/plain")

    public Response getTextFile() {


        File file = new File(TXT_FILE);


        ResponseBuilder response = Response.ok((Object) file);

        response.header("Content-Disposition", "attachment; filename=\"test_text_file.txt\"");

        return response.build();


    }


    /**

     *  Download Image File

     */

    @GET

    @Path("/images")

    @Produces("image/png")

    public Response getImageFile() {


        File file = new File(IMAGE_FILE);


        ResponseBuilder response = Response.ok((Object) file);

        response.header("Content-Disposition", "attachment; filename=\"test_image_file.png\"");

        return response.build();


    }


    /**

     *  Download PDF File

     */

    @GET

    @Path("/pdf")

    @Produces("application/pdf")

    public Response getPDF() {


        File file = new File(PDF_FILE);


        ResponseBuilder response = Response.ok((Object) file);

        response.header("Content-Disposition", "attachment; filename=\"test_PDF_file.pdf\"");

        return response.build();


    }


    /**

     *  Download Excel File

     */

    @GET

    @Path("/excel")

    @Produces("aapplication/vnd.ms-excel")

    public Response getExcell() {


        File file = new File(EXCEL_FILE);


        ResponseBuilder response = Response.ok((Object) file);

        response.header("Content-Disposition", "attachment; filename=\"test_excel_file.xls\"");

        return response.build();

    }


}


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.