Friday, 25 September 2015

RESTful JAX-RS File Download Example

RESTful JAX-RS File Download Example

We can download text files, image files, pdf files, excel files in java by JAX-RS API. To do so we need to write few lines of code only. Here, we are using jersey implementation for developing JAX-RS file download examples.
You need to specify different content type to download different files. The @Produces annotation is used to specify the type of file content.
  1. @Produces("text/plain"): for downloading text file.
  2. @Produces("image/png"): for downloading png image file.
  3. @Produces("application/pdf"): for downloading PDF file.
  4. @Produces("application/vnd.ms-excel"): for downloading excel file.
  5. @Produces("application/msword"): for downloading ms word file.
.

JAX-RS Text File Download

File: FileDownloadService.java
  1. package com.javatpoint.rest;  
  2. import java.io.File;  
  3. import javax.ws.rs.GET;  
  4. import javax.ws.rs.Path;  
  5. import javax.ws.rs.Produces;  
  6. import javax.ws.rs.core.Response;  
  7. import javax.ws.rs.core.Response.ResponseBuilder;  
  8. @Path("/files")  
  9. public class FileDownloadService {  
  10.     private static final String FILE_PATH = "c:\\myfile.txt";  
  11.     @GET  
  12.     @Path("/txt")  
  13.     @Produces("text/plain")  
  14.     public Response getFile() {  
  15.         File file = new File(FILE_PATH);  
  16.    
  17.         ResponseBuilder response = Response.ok((Object) file);  
  18.         response.header("Content-Disposition","attachment; filename=\"javatpoint_file.txt\"");  
  19.         return response.build();  
  20.    
  21.     }  
  22.  }  
File: web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3. xmlns="http://java.sun.com/xml/ns/javaee"   
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   
  6. id="WebApp_ID" version="3.0">  
  7.  <servlet>  
  8.     <servlet-name>Jersey REST Service</servlet-name>  
  9.     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
  10.     <init-param>  
  11.         <param-name>jersey.config.server.provider.packages</param-name>  
  12.         <param-value>com.javatpoint.rest</param-value>  
  13.     </init-param>  
  14.     <load-on-startup>1</load-on-startup>  
  15.   </servlet>  
  16.   <servlet-mapping>  
  17.     <servlet-name>Jersey REST Service</servlet-name>  
  18.     <url-pattern>/rest/*</url-pattern>  
  19.   </servlet-mapping>  
  20. </web-app>   
File: index.html
  1. <a href="rest/files/txt">Download Text File</a>  
Now run this application on server, you will see the following output:
Output:
jax rs file download text example



JAX-RS Image File Download

File: FileDownloadService.java
  1. package com.javatpoint.rest;  
  2. import java.io.File;  
  3. import javax.ws.rs.GET;  
  4. import javax.ws.rs.Path;  
  5. import javax.ws.rs.Produces;  
  6. import javax.ws.rs.core.Response;  
  7. import javax.ws.rs.core.Response.ResponseBuilder;  
  8. @Path("/files")  
  9. public class FileDownloadService {  
  10.     private static final String FILE_PATH = "c:\\myimage.png";  
  11.     @GET  
  12.     @Path("/image")  
  13.     @Produces("image/png")  
  14.     public Response getFile() {  
  15.         File file = new File(FILE_PATH);  
  16.         ResponseBuilder response = Response.ok((Object) file);  
  17.         response.header("Content-Disposition","attachment; filename=\"javatpoint_image.png\"");  
  18.         return response.build();  
  19.    
  20.     }  
  21.  }  
File: web.xml
Same as above example.
File: index.html
  1. <a href="rest/files/image">Download Image File</a>  


JAX-RS PDF File Download

File: FileDownloadService.java
  1. package com.javatpoint.rest;  
  2. import java.io.File;  
  3. import javax.ws.rs.GET;  
  4. import javax.ws.rs.Path;  
  5. import javax.ws.rs.Produces;  
  6. import javax.ws.rs.core.Response;  
  7. import javax.ws.rs.core.Response.ResponseBuilder;  
  8. @Path("/files")  
  9. public class FileDownloadService {  
  10.     private static final String FILE_PATH = "c:\\mypdf.pdf";  
  11.     @GET  
  12.     @Path("/pdf")  
  13.     @Produces("application/pdf")  
  14.     public Response getFile() {  
  15.         File file = new File(FILE_PATH);  
  16.         ResponseBuilder response = Response.ok((Object) file);  
  17.         response.header("Content-Disposition","attachment; filename=\"javatpoint_pdf.pdf\"");  
  18.         return response.build();  
  19.     }  
  20.  }  
File: web.xml
Same as above example.
File: index.html
  1. <a href="rest/files/pdf">Download PDF File</a>  

No comments:

Post a Comment