Image Servlet

I am trying to access images from the WEB-INF directory through a servlet because I want to protect the images and I am finding its not as easy as I thought. Could someone please point me to some sample code for creating an Image Servlet. Thanks.Pleeaasseeee Heeeelppppp!!!
[287 byte] By [Wasif001a] at [2007-11-26 18:41:51]
# 1

Hi There,

Check the code below which outputs an Image using a Servlet.It makes use of ServletContext.getRealPath(String) method to get the image file.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

// Get the absolute path of the image

ServletContext sercon = getServletContext();

String filename = sercon.getRealPath("/WEB-INF/image.gif");

// Get the MIME type of the image

String mimeType = sercon.getMimeType(filename);

if (mimeType == null) {

sercon.log("Could not get information about MIME type of "+filename);

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

return;

}

// Set content type

response.setContentType(mimeType);

// Set content size

File file = new File(filename);

response.setContentLength((int)file.length());

// Open the file and output streams

FileInputStream in = new FileInputStream(file);

OutputStream out = response.getOutputStream();

// Copy the contents of the file to the output stream

byte[] buf = new byte[1024];

int count = 0;

while ((count = in.read(buf)) >= 0) {

out.write(buf, 0, count);

}

in.close();

out.close();

}

Hope this might be of some help.... :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-9 6:15:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks a lot for the solution. But this solution brings another major problem. Now the webpage consists of bytes to make up an image and I cannot include any html on the page to have other content on the page aside from the single image.

Thanks for the solution anyway as this has led me to come up with other ways of getting the task done... just not in an efficient way.

For example: using getRealPath() works, but now my html source looks like this:

"<img src="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\WEB-INF\return.gif">"

Is there anyway of getting this done without revealing the entire path and location of the file?

Wasif001a at 2007-7-9 6:15:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Oops...

Using getRealPath() DOES NOT WORK because the source looks like this:

"<img src="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\WEB-INF\return.gif">"

which means that it will only work on the local machine where my testing was being done!!!

Is there any other way of doing this aside from sending the page content in bytes because I want other HTML on the page as well?

Thanks in advance!

Wasif001a at 2007-7-9 6:15:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Okay, I am new to this so it took a while, but I got the code above provided by RahulSharna to work perfectly. Thanks again!!!
Wasif001a at 2007-7-9 6:15:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
thank you for your code. just a small question. i would like to display multiple images in a servlet, how should i proceed?regards
vital30a at 2007-7-9 6:15:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

well friend the thing which you have to understand here is that the code provided above would only create an image response. Inorder to display mutliple image reponse use a workaround something like the one provided in your JSP....

ImageServlet.java

==============

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

String reqParam = new String();

String path = new String();

try{

reqParam = request.getParameter("req");

} catch(Exception exp){

}

if(reqParam.equals("1"))

path = "/WEB-INF/image.gif";

else if(reqParam.equals("2"))

path = "/WEB-INF/image1.gif";

else if(reqParam.equals("3"))

path = "/WEB-INF/image2.gif";

.........

.........

..........

// Get the absolute path of the image

ServletContext sercon = getServletContext();

String filename = sercon.getRealPath(path);

// Get the MIME type of the image

String mimeType = sercon.getMimeType(filename);

if (mimeType == null) {

sercon.log("Could not get information about MIME type of "+filename);

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

return;

}

// Set content type

response.setContentType(mimeType);

// Set content size

File file = new File(filename);

response.setContentLength((int)file.length());

// Open the file and output streams

FileInputStream in = new FileInputStream(file);

OutputStream out = response.getOutputStream();

// Copy the contents of the file to the output stream

byte[] buf = new byte[1024];

int count = 0;

while ((count = in.read(buf)) >= 0) {

out.write(buf, 0, count);

}

in.close();

out.close();

}

& at your html/jsp end try to restructure code like

<%@ page language="java" %>

<html>

<head>

<title>Display</title>

</head>

<body>

<img src="ImageServlet?req=1"/>

<img src="ImageServlet?req=2"/>

<img src="ImageServlet?req=3"/>

............

..............

</body>

Hope this might help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-9 6:15:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...