In this example we will see how
we can download file such as image file, pdf file, excel file, text file etc. 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.
Steps:
1. 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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>com.tushar.jersey.Project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.tushar.jersey.Project</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
|
2. FileDownload.java
|
package
com.tushar.jersey.Project;
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 FileDownload {
private static final String FILE_PATH_TEXT = "d:\\textTest.txt";
private static final String FILE_PATH_IMAGE = "d:\\image.jpg";
private static final String FILE_PATH_PDF = "d:\\pdfTest.pdf";
@GET
@Path("/txt")
@Produces("text/plain")
public Response getTextFile() {
File file = new
File(FILE_PATH_TEXT);
ResponseBuilder response = Response.ok((Object)
file);
response.header("Content-Disposition","attachment;
filename=\"javatpoint_file.txt\"");
return
response.build();
}
@GET
@Path("/image")
@Produces("image/png")
public Response getImageFile() {
File file = new
File(FILE_PATH_IMAGE);
ResponseBuilder response = Response.ok((Object)
file);
response.header("Content-Disposition","attachment;
filename=\"javatpoint_image.png\"");
return
response.build();
}
@GET
@Path("/pdf")
@Produces("application/pdf")
public Response getPDFFile() {
File file = new
File(FILE_PATH_PDF);
ResponseBuilder response = Response.ok((Object)
file);
response.header("Content-Disposition","attachment;
filename=\"javatpoint_pdf.pdf\"");
return
response.build();
}
}
|
3. Download Text file
URL : http://localhost:8080/com.tushar.jersey.Project/rest/file/txt
4. Download Image file
URL : http://localhost:8080/com.tushar.jersey.Project/rest/file/image

No comments:
Post a Comment