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.
- @Produces("text/plain"): for downloading text file.
- @Produces("image/png"): for downloading png image file.
- @Produces("application/pdf"): for downloading PDF file.
- @Produces("application/vnd.ms-excel"): for downloading excel file.
- @Produces("application/msword"): for downloading ms word file.
.
JAX-RS Text File Download
File: FileDownloadService.java
- package com.javatpoint.rest;
- 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("/files")
- public class FileDownloadService {
- private static final String FILE_PATH = "c:\\myfile.txt";
- @GET
- @Path("/txt")
- @Produces("text/plain")
- public Response getFile() {
- File file = new File(FILE_PATH);
-
- ResponseBuilder response = Response.ok((Object) file);
- response.header("Content-Disposition","attachment; filename=\"javatpoint_file.txt\"");
- return response.build();
-
- }
- }
File: 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"
- 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.javatpoint.rest</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>
- </web-app>
File: index.html
- <a href="rest/files/txt">Download Text File</a>
Now run this application on server, you will see the following output:
Output:
JAX-RS Image File Download
File: FileDownloadService.java
- package com.javatpoint.rest;
- 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("/files")
- public class FileDownloadService {
- private static final String FILE_PATH = "c:\\myimage.png";
- @GET
- @Path("/image")
- @Produces("image/png")
- public Response getFile() {
- File file = new File(FILE_PATH);
- ResponseBuilder response = Response.ok((Object) file);
- response.header("Content-Disposition","attachment; filename=\"javatpoint_image.png\"");
- return response.build();
-
- }
- }
File: web.xml
Same as above example.
File: index.html
- <a href="rest/files/image">Download Image File</a>
JAX-RS PDF File Download
File: FileDownloadService.java
- package com.javatpoint.rest;
- 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("/files")
- public class FileDownloadService {
- private static final String FILE_PATH = "c:\\mypdf.pdf";
- @GET
- @Path("/pdf")
- @Produces("application/pdf")
- public Response getFile() {
- File file = new File(FILE_PATH);
- ResponseBuilder response = Response.ok((Object) file);
- response.header("Content-Disposition","attachment; filename=\"javatpoint_pdf.pdf\"");
- return response.build();
- }
- }
File: web.xml
Same as above example.
File: index.html
- <a href="rest/files/pdf">Download PDF File</a>
No comments:
Post a Comment