Saturday, 26 September 2015

RESTful JAX-RS File Upload Example

RESTful JAX-RS File Upload Example

Like download in previous page, we can easily upload a file such as image file, pdf file, excel file, text file etc.
The @FormDataParam("file") annotation is used to mention file parameter in the service class. The @Consumes(MediaType.MULTIPART_FORM_DATA) is used to provide information of the file upload.
To upload file using JAX-RS API, we are using jersey implementation.

Click me to download jersey jar files.

To upload file through jersey implementation, you need to provide extra configuration entry in web.xml file.
  1.     <init-param>  
  2.     <param-name>jersey.config.server.provider.classnames</param-name>  
  3.     <param-value>org.glassfish.jersey.filter.LoggingFilter;  
  4.      org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>  
  5. </init-param>  
Let's see the complete code to upload file using RESTful JAX-RS API.

JAX-RS File Upload

File: FileUploadService.java
  1. package com.javatpoint.rest;  
  2. import java.io.File;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import javax.ws.rs.Consumes;  
  7. import javax.ws.rs.POST;  
  8. import javax.ws.rs.Path;  
  9. import javax.ws.rs.core.MediaType;  
  10. import javax.ws.rs.core.Response;  
  11. import org.glassfish.jersey.media.multipart.FormDataContentDisposition;  
  12. import org.glassfish.jersey.media.multipart.FormDataParam;  
  13. @Path("/files")  
  14. public class FileUploadService {  
  15.     @POST  
  16.     @Path("/upload")  
  17.     @Consumes(MediaType.MULTIPART_FORM_DATA)  
  18.     public Response uploadFile(  
  19.             @FormDataParam("file") InputStream uploadedInputStream,  
  20.             @FormDataParam("file") FormDataContentDisposition fileDetail) {  
  21.             String fileLocation = "e://" + fileDetail.getFileName();  
  22.                     //saving file  
  23.             try {  
  24.                 FileOutputStream out = new FileOutputStream(new File(fileLocation));  
  25.                 int read = 0;  
  26.                 byte[] bytes = new byte[1024];  
  27.                 out = new FileOutputStream(new File(fileLocation));  
  28.                 while ((read = uploadedInputStream.read(bytes)) != -1) {  
  29.                     out.write(bytes, 0, read);  
  30.                 }  
  31.                 out.flush();  
  32.                 out.close();  
  33.             } catch (IOException e) {e.printStackTrace();}  
  34.             String output = "File successfully uploaded to : " + fileLocation;  
  35.             return Response.status(200).entity(output).build();  
  36.         }  
  37.   }  
File: web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  3. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  4.  <servlet>  
  5.     <servlet-name>Jersey REST Service</servlet-name>  
  6.     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
  7.      <init-param>  
  8.         <param-name>jersey.config.server.provider.packages</param-name>  
  9.         <param-value>com.javatpoint.rest</param-value>  
  10.     </init-param>  
  11.     <init-param>  
  12.     <param-name>jersey.config.server.provider.classnames</param-name>  
  13.     <param-value>org.glassfish.jersey.filter.LoggingFilter;  
  14.      org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>  
  15. </init-param>  
  16.     <load-on-startup>1</load-on-startup>  
  17.   </servlet>  
  18.   <servlet-mapping>  
  19.     <servlet-name>Jersey REST Service</servlet-name>  
  20.     <url-pattern>/rest/*</url-pattern>  
  21.   </servlet-mapping>  
  22. </web-app>   
File: index.html
  1. <h2>File Upload Example</h2>  
  2. <form action="rest/files/upload" method="post" enctype="multipart/form-data">  
  3.        <p>  
  4.         Select a file : <input type="file" name="file" size="45" />  
  5.        </p>  
  6.        <input type="submit" value="Upload File" />  
  7. </form>  
Now run this application on server, you will see the following output:
Output:
  1. File successfully uploaded to e://myimage.png  

No comments:

Post a Comment