Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

JAX-RS File Download


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.

Click me to download jersey jar files.


JAX-RS Text File Download

File: FileDownloadService.java

  1. package com.Kreationnext.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.         ResponseBuilder response = Response.ok((Object) file);
  17.         response.header(“Content-Disposition”,“attachment; filename=\”Kreationnext_file.txt\””);
  18.         return response.build();
  19.     }
  20.  }

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.Kreationnext.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-txt

JAX-RS Image File Download

File: FileDownloadService.java

  1. package com.Kreationnext.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=\”Kreationnext_image.png\””);
  18.         return response.build();
  19.     }
  20.  }

File: web.xml

Same as above example.

File: index.html

  1. <a href=“rest/files/image”>Download Image File</a>

Click me to download this example


JAX-RS PDF File Download

File: FileDownloadService.java

  1. package com.Kreationnext.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=\”Kreationnext_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>

Click me to download this example

Comments

comments