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
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?
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!
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