Need help w. Image Servlet.

S

Steve Burrus

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

Oscar kind

Steve Burrus said:
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).
 
S

Steve Burrus

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) :
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?
 
J

Jon Martin Solaas

Steve said:
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) :
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 said:
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 ...
 
S

Steve Burrus

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?? :
//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();
}
}"<<
 
A

Andrei

Steve Burrus said:
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?? :
//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();
}
}"<<
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
 
J

Jon Martin Solaas

Steve said:
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 ....
//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();
}
}"<<
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 ... ;-)
 
S

Steve Burrus

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:>
 
A

Andrei

Steve Burrus said:
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:>

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top