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
>>"// 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?
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
>>"// 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
try :
File file = new File(realPath);
or better yet :
sc.getResourceAsStream( realPath );
Regards,
Andrei
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 ... ;-)
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