In previous post, I explained how to download a file. In this post I am going to show you,
how to send images to the client using server itself.
Following
snippet is used to send images to client.
@GET
@Path("/{id}")
@Produces("image/png")
public
Response downloadImage(@PathParam("id") long empId) {
System.out.println("Hello");
File file = new
File("/Users/harikrishna_gurram/Decentralized.png");
ResponseBuilder response =
Response.ok((Object) file);
response.header("Content-Disposition","attachment;
filename=\"fileName.png\"");
return response.build();
}
I hard coded
the image file. You can update as per your application needs.
Following
step-by-step procedure that explain complete working application.
Step 1: Create dynamic web project ‘jersey_imageapp’ in
eclipse.
File ->
New -> Dynamic Web project.
Give the
project name as ‘jersey_imageapp’, press next.
Select
‘Generate web.xml deployment descriptor’.
Press
Finish.
Step 2: Convert this project to maven project.
Right click
on the project -> Configure -> Convert To Maven Project.
Following is
the complete project structure.
Step 3: Update web.xml like below.
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"> <display-name>jersey_imageapp</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 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.jersey_imageapp</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>
Step 4: Update pom.xml for maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jersey_imageapp</groupId> <artifactId>jersey_imageapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.21</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>2.21</version> </dependency> </dependencies> </project>
Step 5: Create package com.jersey_imageapp. Define class
ImageUtil.
package com.jersey_imageapp; import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/images") public class ImageUtil { @GET @Path("/{id}") @Produces("image/png") public Response downloadImage(@PathParam("id") long empId) { System.out.println("Hello"); File file = new File("/Users/harikrishna_gurram/Decentralized.png"); ResponseBuilder response = Response.ok((Object) file); response.header("Content-Disposition", "attachment; filename=\"fileName.png\""); return response.build(); } }
Following is
the complete project structure.
Run
application on server and hit following url, you will get the image.
References
No comments:
Post a Comment