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.
- <init-param>
- <param-name>jersey.config.server.provider.classnames</param-name>
- <param-value>org.glassfish.jersey.filter.LoggingFilter;
- org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
- </init-param>
Let's see the complete code to upload file using RESTful JAX-RS API.
JAX-RS File Upload
File: FileUploadService.java
- package com.javatpoint.rest;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- 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 org.glassfish.jersey.media.multipart.FormDataContentDisposition;
- import org.glassfish.jersey.media.multipart.FormDataParam;
- @Path("/files")
- public class FileUploadService {
- @POST
- @Path("/upload")
- @Consumes(MediaType.MULTIPART_FORM_DATA)
- public Response uploadFile(
- @FormDataParam("file") InputStream uploadedInputStream,
- @FormDataParam("file") FormDataContentDisposition fileDetail) {
- String fileLocation = "e://" + fileDetail.getFileName();
-
- try {
- FileOutputStream out = new FileOutputStream(new File(fileLocation));
- int read = 0;
- byte[] bytes = new byte[1024];
- out = new FileOutputStream(new File(fileLocation));
- while ((read = uploadedInputStream.read(bytes)) != -1) {
- out.write(bytes, 0, read);
- }
- out.flush();
- out.close();
- } catch (IOException e) {e.printStackTrace();}
- String output = "File successfully uploaded to : " + fileLocation;
- return Response.status(200).entity(output).build();
- }
- }
File: web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <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
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
- <servlet>
- <servlet-name>Jersey REST Service</servlet-name>
- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
- <init-param>
- <param-name>jersey.config.server.provider.packages</param-name>
- <param-value>com.javatpoint.rest</param-value>
- </init-param>
- <init-param>
- <param-name>jersey.config.server.provider.classnames</param-name>
- <param-value>org.glassfish.jersey.filter.LoggingFilter;
- org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Jersey REST Service</servlet-name>
- <url-pattern>/rest/*</url-pattern>
- </servlet-mapping>
- </web-app>
File: index.html
- <h2>File Upload Example</h2>
- <form action="rest/files/upload" method="post" enctype="multipart/form-data">
- <p>
- Select a file : <input type="file" name="file" size="45" />
- </p>
- <input type="submit" value="Upload File" />
- </form>
Now run this application on server, you will see the following output:
Output:
- File successfully uploaded to e://myimage.png
No comments:
Post a Comment