In this tutorial we will learn how to develop JAX – RS Restful application to download different files.
First we see how to download text file example .
Text File download example
——————————————————————————————————————————–
1. Create Dynamic web project in Eclipse.
2. Write Java Resource classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.connect2java.restfulfiledownloadexample; import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/downloadfiles") public class RestfulFiledownloadExample { private static final String path = "c:\\log.txt"; @GET @Path("/downloadtxt") @Produces("text/plain") public Response getFile() { File file = new File(path); ResponseBuilder response = Response.ok((Object) file); response.header("Content-Disposition","attachment; filename=\"newfile.txt\""); return response.build(); } } |
3. Configure the Jersey Servlet in web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?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"> <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.connect2java.restfulfiledownloadexample</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> <welcome-file-list> <welcome-file>download.html</welcome-file> </welcome-file-list> </web-app> |
4. Run the Example
After Deploying the application access the url http://localhost:8080/RestfulFileDownloadExample/ and click on “Click Here to Download File” , so the below File Download window will appear.
OUT PUT:
NOTE : I have not included all the required jersey jars in the source code please download it from here and copy to lib folder.
Download Source Code
Excel File download example
——————————————————————————————————————————–
if you want to down load excel file then our resource class will like as below.
juts change the @Produces mimetype and response header properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.connect2java.restfulfiledownloadexample; import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/downloadfiles") public class RestfulFiledownloadExample { private static final String path = "c:\\log.xls"; @GET @Path("/downloadtxt") @Produces("application/vnd.ms-excel") public Response getFile() { File file = new File(path); ResponseBuilder response = Response.ok((Object) file); response.header("Content-Disposition","attachment; filename=\"newfile.xls\""); return response.build(); } } |
Image File download example
——————————————————————————————————————————–
if you want to down load image file then our resource class will like as below.
juts change the @Produces mime type and response header properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.connect2java.restfulfiledownloadexample; import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/downloadfiles") public class RestfulFiledownloadExample { private static final String path = "c:\\test.png"; @GET @Path("/downloadtxt") @Produces("image/png") public Response getFile() { File file = new File(path); ResponseBuilder response = Response.ok((Object) file); response.header("Content-Disposition","attachment; filename=\"newfile.png\""); return response.build(); } } |
PDF File download example
——————————————————————————————————————————–
if you want to down load image file then our resource class will like as below.
juts change the @Produces mime type and response header properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.connect2java.restfulfiledownloadexample; import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/downloadfiles") public class RestfulFiledownloadExample { private static final String path = "c:\\test.pdf"; @GET @Path("/downloadtxt") @Produces("application/pdf") public Response getFile() { File file = new File(path); ResponseBuilder response = Response.ok((Object) file); response.header("Content-Disposition","attachment; filename=\"newfile.pdf\""); return response.build(); } } |
Happy learning ……….. 🙂
Very nice…..And easy to understand…
Thanks Ramesh….
i love this website a lot!
fifa coins http://u4fifa.tumblr.com/post/144837166157/several-fifa-coins-services-for-the-coming-fifa-17