Saturday, 23 August 2014

File Upload and Download Service using Jersey


In this tutorial you will learn how to create File Upload and Download restful Web service
service using jersey.
JAX-RS provide a meda type MULTIPART_FORM_DATA for consuming blob through html form.
UploadService will consume blob(pdf,image,etc) through form and save it to disk.

UploadFileService.java

@Path("/uploader")
public class UploadFileService {

@POST
@Path("/post-blob")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {

String uploadedFileLocation = System.getProperty("user.dir")+"/"+ fileDetail.getFileName();

writeToDisk(uploadedInputStream, uploadedFileLocation);

String output = "File uploaded to : " + uploadedFileLocation;

return Response.status(200).entity(output).build();

}

private void writeToDisk(InputStream uploadedInputStream,
String uploadedFileLocation) {

try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
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();
}

}


}

The Download Service will download the file from the disk and return Streaming Output fo the clients of Download Service.

DownloadService.java

@Path("/downloader")
public class DownloadFileService {
@GET
@Path("/receive-blob")
@Produces(MediaType.APPLICATION_OCTET_STREAM)

public StreamingOutput getPDF(@QueryParam("fileName") final String fileName) throws Exception {
  
return new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException,WebApplicationException {
try{
if (null!=fileName) {
InputStream fileStream=new FileInputStream(new File(System.getProperty("user.dir")+"/"+fileName));
byte[] buffer = new byte[2048];
int length = 0;
while ((length = fileStream.read(buffer)) != -1) {
output.write(buffer, 0, length);
}

}
}catch(Exception e){

}finally{
  output.close();
   
}

}
   
   };
}


}

Client for download service

public class JerseyClientFileDownload {

//Enter the file you want to download
private static String FILE_NAME="FolderSyncGetChnages.jmx";
public static void main(String[] args) throws IOException {
JerseyClientFileDownload jc=new JerseyClientFileDownload();
 jc.getPDFStream(FILE_NAME);

 
}
 
 
 public InputStream getPDFStream(String fileName) throws IOException  {
 Client client = Client.create();
 
WebResource webResource = client.resource("http://localhost:8080/RestFileServer/service/downloader/receive-blob");
   ClientResponse response = webResource.queryParam("fileName",fileName).type("application/octet_stream").get(ClientResponse.class);
   System.out.println(response.getEntityInputStream());
 
   OutputStream os = new FileOutputStream(new File("/home/kuntal/practice"+fileName));
   byte[] buffer = new byte[2048];
int length = 0;
InputStream inputStream =response.getEntityInputStream();
while ((length = inputStream.read(buffer)) !=-1) {
os.write(buffer, 0, length);
}
   return response.getEntityInputStream();
}
 
 

}


The Html form to upload the file to UploadService.

Upload.html

<html>
<body>
<h1>File Upload with Jersey</h1>

<form action="service/uploader/post-blob" method="post" enctype="multipart/form-data">

  <p>
Select a file : <input type="file" name="file" size="45" />
  </p>

 <b></b> <input type="submit" value="Upload" /></b>
</form>

</body>

</html>


Now you have RestFileServer ready to upload and download file for you :) Hope it helps you!

Download Code

No comments:

Post a Comment