We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 1 December 2013

File Upload using JAX-RS RESTful Webservice


In this tutorial we are going to see how to upload file using Jersey RESTful web service
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder
addjar
and add mimepull-1.4.jar also to upload multi part data
4.Create file-upload.jsp under WebContent folder.
file-upload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="rest/fileupload/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload">
</form>
</body>
</html>

5. Create package called com.javatutorialscorner.jaxrs.uploadfile under RESTful-WebService
6. Create Java class FileUploadService under com.javatutorialscorner.jaxrs.uploadfile package
FileUploadService.java

package com.javatutorialscorner.jaxrs.uploadfile;

import java.io.File;
import java.io.FileOutputStream;
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 FileUploadService {

String response = "";

@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream inputStream,
@FormDataParam("file") FormDataContentDisposition file) {

try {
final String FILE_DESTINATION = "C://jtc//" + file.getFileName();
File f = new File(FILE_DESTINATION);
OutputStream outputStream = new FileOutputStream(f);
int size = 0;
byte[] bytes = new byte[1024];
while ((size = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, size);
}
outputStream.flush();
outputStream.close();

response = "File uploaded " + FILE_DESTINATION;
} catch (Exception e) {
e.printStackTrace();
}

return Response.status(200).entity(response).build();
}
}

7.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
<display-name>JAX-RS-Path</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.javatutorialscorner.jaxrs.uploadfile</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL
Now you can run file-upload.jsp by calling following URL

http://localhost:8080/RESTful-WebService/file-upload.jsp

upload file to

 http://localhost:8080/RESTful-WebService/rest/fileupload/upload

Output
http://localhost:8080/RESTful-WebService/file-upload.jsp

file upload after file uploaded response from server

output

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: File Upload using JAX-RS RESTful Webservice Rating: 5 Reviewed By: eHowToNow