Need help w. Image Servlet.

Discussion in 'Java' started by Steve Burrus, Oct 16, 2004.

  1. Steve Burrus

    Steve Burrus Guest

    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.
    Steve Burrus, Oct 16, 2004
    #1
    1. Advertising

  2. Steve Burrus

    Oscar kind Guest

    Steve Burrus <> wrote:
    > 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.


    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
    Oscar kind, Oct 16, 2004
    #2
    1. Advertising

  3. Steve Burrus

    Steve Burrus Guest

    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?


    Oscar kind wrote:
    >
    > Where is the image stored? The easiest way is the classpath, as you can
    > then use Class#getResource(String) and Class#getResourceAsStream(String).
    >
    >
    Steve Burrus, Oct 16, 2004
    #3
  4. 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.

    > 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?
    >
    >
    > Oscar kind wrote:
    >
    >>
    >> Where is the image stored? The easiest way is the classpath, as you can
    >> then use Class#getResource(String) and Class#getResourceAsStream(String).
    >>
    >>


    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¤h0tm4i1
    Jon Martin Solaas, Oct 17, 2004
    #4
  5. Steve Burrus

    Steve Burrus Guest

    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


    > 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 ...
    >
    Steve Burrus, Oct 17, 2004
    #5
  6. Steve Burrus

    Andrei Guest

    Steve Burrus <> wrote in message news:<KIAcd.7195$q%>...
    > 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

    >
    > > 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 ...
    > >


    try :
    File file = new File(realPath);

    or better yet :
    sc.getResourceAsStream( realPath );


    Regards,
    Andrei
    Andrei, Oct 18, 2004
    #6
  7. 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 ... ;-)
    Jon Martin Solaas, Oct 18, 2004
    #7
  8. Steve Burrus

    Steve Burrus Guest

    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 wrote:>
    >
    > try :
    > File file = new File(realPath);
    >
    > or better yet :
    > sc.getResourceAsStream( realPath );
    >
    >
    > Regards,
    > Andrei
    Steve Burrus, Oct 18, 2004
    #8
  9. Steve Burrus

    Andrei Guest

    Steve Burrus <> wrote in message news:<IWVcd.7246$>...
    > 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 wrote:>
    > >
    > > try :
    > > File file = new File(realPath);
    > >
    > > or better yet :
    > > sc.getResourceAsStream( realPath );
    > >
    > >
    > > Regards,
    > > Andrei


    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
    Andrei, Oct 19, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    2,411
    Andy Fish
    Dec 17, 2003
  2. Sean Clarke
    Replies:
    1
    Views:
    1,891
    Sudsy
    Jan 7, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    1,993
    Jack Jia
    Apr 4, 2004
  4. chamikara
    Replies:
    1
    Views:
    49,858
  5. ppcguy
    Replies:
    1
    Views:
    604
    Andrea Desole
    Aug 8, 2005
Loading...

Share This Page