Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help w. Image Servlet.

8 views
Skip to first unread message

Steve Burrus

unread,
Oct 15, 2004, 9:52:40 PM10/15/04
to
I need some degree of help/assistance with trying to see an image in a
servlet! I am trying to use the double method of
"getServletContext().getRealPath()" to do this! But I seem to always get
the compiler error msg. pointing to that "getRealPath()" method. I have
tried every which way to get it right with the path, I guess an absolute
path to the image resource that it wants as the method argument, right?
I can submit my code for this servlet, if someone wants to see it.

Oscar kind

unread,
Oct 16, 2004, 3:02:50 AM10/16/04
to

Where is the image stored? The easiest way is the classpath, as you can
then use Class#getResource(String) and Class#getResourceAsStream(String).


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2

Steve Burrus

unread,
Oct 16, 2004, 5:09:10 PM10/16/04
to
Oscar, thank you very much for being THE ONLY RESPONSE back to me so
far, but I guess that i should be grateful/thankful for that! Listen, I
ended my post on this subject with an offer to submit my servlet code,
so here it is for YOU, at least,to look at!! (Incidentally, I got this
code from a web page which someone referred me to to try to help me with
tbis problem) :

>>"// This method is called by the servlet container to process a GET
request.
public void doGet(HttpServletRequest req, HttpServletResponse resp)

throws IOException {*
// Get the absolute path of the image
ServletContext sc = getServletContext();
String filename = sc.getRealPath("image.gif");

// Get the MIME type of the image
String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
sc.log("Could not get MIME type of "+filename);
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}

// Set content type
resp.setContentType(mimeType);

// Set content size
File file = new File(filename);
resp.setContentLength((int)file.length());

// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.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();
} "<<
* The asterisk up at the top of the code means that i noticed that it
has only the IOException and not the usual ServletException also in the
start of the "doGet()" method, is that correct?

Jon Martin Solaas

unread,
Oct 17, 2004, 5:21:35 AM10/17/04
to
Steve Burrus wrote:
> Oscar, thank you very much for being THE ONLY RESPONSE back to me so
> far, but I guess that i should be grateful/thankful for that!

Posting code *and* error message usually helps a lot. Whining does not.

If the problem is a *compiler* error, as described in the first posting,
the actual problem is not where the images are stored at all, the
compiler doesn't care about that.

If the problem occurs at runtime, it might have something to do with the
fact that you are using a file and not a path as parameter to the
getRealPath() method. Look in the javadocs, you are supposed to feed it
a relative path, and the servlet container will prepend/prefix it so
that it makes up an absolute path instead. I suppose some quasi code
could look like

String realPath = getServletContext().getRealPath("images/") + "image.gif";

Now realPath would be something like
"/home/tomcat/applications/my_application/public_html/images/image.gif"
or something ...

--
jonmartin.solaas士0tm4i1

Steve Burrus

unread,
Oct 17, 2004, 4:35:54 PM10/17/04
to
Jon, I tried this code here to see the image ("images/31.jpeg") in a
servlet, but it still failed on me at runtime with an exception saying
"File Not Found"! What now am I doing wrong?? :

>>"// Here is the code again that i "lifted" off of a web page I was
//pointed to.

package org.fun.servlets;

import java.io.*;
import java.awt.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Outputstream_Img extends HttpServlet {


// This method is called by the servlet container to process a

//GET request.


public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {

// Get the absolute path of the image
ServletContext sc = getServletContext();

String realPath = getServletContext().getRealPath("images/") +

"31.jpeg";

// Get the MIME type of the image

String mimeType = sc.getMimeType("images/31.jpeg");
if (mimeType == null) {
sc.log("Could not get MIME type of " +"images/31.jpeg");
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}

// Set content type
resp.setContentType(mimeType);

// Set content size
File file = new File("images/31.jpeg");
resp.setContentLength((int)file.length());

// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.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();
}
}"<<

Jon Martin Solaas wrote:
> snip

Andrei

unread,
Oct 18, 2004, 3:17:59 AM10/18/04
to
Steve Burrus <bur...@swbell.net> wrote in message news:<KIAcd.7195$q%7.5...@newssvr11.news.prodigy.com>...

try :
File file = new File(realPath);

or better yet :
sc.getResourceAsStream( realPath );


Regards,
Andrei

Jon Martin Solaas

unread,
Oct 18, 2004, 10:22:28 AM10/18/04
to
Steve Burrus wrote:
> Jon, I tried this code here to see the image ("images/31.jpeg") in a
> servlet, but it still failed on me at runtime with an exception saying
> "File Not Found"! What now am I doing wrong?? :


See below ....

>
> >>"// Here is the code again that i "lifted" off of a web page I was
> //pointed to.
>
> package org.fun.servlets;
>
> import java.io.*;
> import java.awt.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class Outputstream_Img extends HttpServlet {
> // This method is called by the servlet container to process a
> //GET request.
> public void doGet(HttpServletRequest req, HttpServletResponse resp)
> throws IOException {
> // Get the absolute path of the image
> ServletContext sc = getServletContext();
> String realPath = getServletContext().getRealPath("images/") +
> "31.jpeg";
>

Why bother to get the realPath when you don't use it later?


> // Get the MIME type of the image

use variable realPath here (line below):

> String mimeType = sc.getMimeType("images/31.jpeg");
> if (mimeType == null) {
> sc.log("Could not get MIME type of " +"images/31.jpeg");
> resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
> return;
> }
>
> // Set content type
> resp.setContentType(mimeType);
>
> // Set content size

use variable realPath here (line below):

> File file = new File("images/31.jpeg");

perhaps ... new File(realPath + "images/31.jpeg"); or something ...

> resp.setContentLength((int)file.length());
>
> // Open the file and output streams
> FileInputStream in = new FileInputStream(file);
> OutputStream out = resp.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();
> }
> }"<<
>
> Jon Martin Solaas wrote:
>
>> snip
>
>
>> If the problem occurs at runtime, it might have something to do with
>> the fact that you are using a file and not a path as parameter to the
>> getRealPath() method. Look in the javadocs, you are supposed to feed
>> it a relative path, and the servlet container will prepend/prefix it
>> so that it makes up an absolute path instead. I suppose some quasi
>> code could look like
>>
>> String realPath = getServletContext().getRealPath("images/") +
>> "image.gif";
>>
>> Now realPath would be something like
>> "/home/tomcat/applications/my_application/public_html/images/image.gif"
>> or something ...
>>

Also step through the code in a debugger, or make prints to System.err
or something, to see what the result from getRealPath really is, so that
you don't use too many or too few slashes, for instance.

Your exception / error message contains information that someone could
have used to tell you exactly what to do. Guess you have to fill inn the
little details yourself now ... ;-)

Steve Burrus

unread,
Oct 18, 2004, 4:44:24 PM10/18/04
to
ANdrei, please refresh my memory as to what is contained in the realPath
variable???!!! Is it the full absolute path to the image resource in my
system or not???!! Please give me the full code for using the
InputStream and the OutputStream for seeing an image in my servlet.
Thanx for helping me.

Andrei

unread,
Oct 19, 2004, 3:04:55 AM10/19/04
to
Steve Burrus <bur...@swbell.net> wrote in message news:<IWVcd.7246$Lk3....@newssvr12.news.prodigy.com>...

here is the code :
public void doGet( HttpServletRequest req, HttpServletResponse resp )
throws IOException
{
// Get servlet context
ServletContext sc = getServletContext();


// Get the MIME type of the image

String mimeType = sc.getMimeType( "images/31.jpeg" );
if ( mimeType == null )
{
sc.log( "Could not get MIME type of " + "images/31.jpeg" );
resp.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
return;
}
// Set content type
resp.setContentType( mimeType );

// read the file
InputStream in = sc.getResourceAsStream( "/images/31.jpeg" );
if ( in == null )
{
sc.log( "Could not read file " + "images/31.jpeg" );
resp.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
return;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();


// 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 );
}

// Set content size
resp.setContentLength( out.size() );
// send data
out.writeTo( resp.getOutputStream() );
}


hope it helps

Andrei

0 new messages