Web Services - SPLessons

Web Services File Upload Example

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Web Services File Upload Example

Web Services File Upload Example

shape Description

The File Uploading concepts are always an interesting task and little difficult task why because while developing the projects downloading files such as image, text are the large task where developer has to import more packages. Here annotations will play the major role to download the particular file. In uploading strategy user needs to add following extra stuff in web.xml file. [xml]<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> [/xml]

shape Example

The following is the directory structure of an application. FileUploadService.java The following are the required files, before starting user need to add all the jersy jar files into an application. [java]package com.splessons.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(); //saving file 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(); } } [/java] The @FormDataParam("file") explanation is utilized to say document argument in the service class. The @Consumes() is utilized to give data of the document transfer. The following are the list of annoatations available in RESTful services. web.xml [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.splessons.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> [/xml] index.html [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> [/html] Now compile the code on the server then the result will be as follows. When uploading the some file then following is the message will be displayed on the server console. [java]File successfully uploaded to : e:// [/java]

Summary

shape Key Points

  • The @consumes is the annotation used to get the media type.
  • The parameter of the query string of an URL is reprecented by @QueryParam annotation