Re: displaying an image from outside the webapps directory

2008-04-01 Thread Kimberly Begley
Great - got it working!
Thanks so much for that again!
Kimberly

On Tue, Apr 1, 2008 at 12:07 AM, Christopher Schultz 
[EMAIL PROTECTED] wrote:

 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1

 Kimberly,

 Kimberly Begley wrote:
 | I was hoping to use the jsp:include tag to include the snippet (no html
 or
 | body tags) - I tried with the same code that Chris sent to open a jpeg -
 | thinking it might be the same solution for the html but have gotten an
 | internal error at the jsp include line.

 The problem is that JSPs use HttpServletResponse.getWriter (because JSPs
 are used to produce text output) while the servlet you write uses
 HttpServletRequest.getOutputStream. The two are incompatible for the
 same request, and getWriter has already been called, to getOutputStream
 fails.

 You have a couple of options:

 * Write another version of OpenFile called OpenTextFile or whatever
 * Modify OpenFile to try getOutputStream first, then getWriter
 * Put a special flag in the request, and have OpenFile detect that flag
 ~  to determine whether getWriter or getOutputStream should be called

 If you want to use Writers instead of Streams, you should probably open
 your file using a Reader instead of a Stream as well.

 Hope that helps,
 - -chris

 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1.4.8 (MingW32)
 Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

 iEYEARECAAYFAkfw8D4ACgkQ9CaO5/Lv0PCzxwCfYMDYRCHCjLxo5bCmq+wiuvMG
 0JAAn3+hBtaGqJx6Oucn/ufDBos+G6bi
 =pp7j
 -END PGP SIGNATURE-

 -
 To start a new topic, e-mail: users@tomcat.apache.org
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




-- 
Kimberly Begley


Re: displaying an image from outside the webapps directory

2008-03-31 Thread Kimberly Begley
Thanks for that I will check it out - I have switched gears and have been
trying to get a c:import to work - now I get the error printed in the middle
of the page saying that:

The requested resource (/test/home/kimberly/Desktop/testhtmlfile1.html) is
not available

where test is the directory under webapps and the path that was passed to
the import statement was just /home/kimberly/Desktop...
Any ideas how to get it to stop prefixing the path with /test?
Thanks!

On Mon, Mar 31, 2008 at 3:13 PM, Vamsavardhana Reddy [EMAIL PROTECTED]
wrote:

 Have you considered using the allowLinking attribute (See
 http://tomcat.apache.org/tomcat-6.0-doc/config/context.html) and creating
 a
 soft-link inside the webapp directory to the directory which is outside of
 webapp?

 ++Vamsi

 On Fri, Mar 21, 2008 at 4:28 PM, Kimberly Begley [EMAIL PROTECTED]
 
 wrote:

  Hi,
  I'm trying to make a jsp display a plot that is a jpeg in a directory
  outside the catalina_home directory - I do not have the option of
  putting it in the webapp directory (there's 1000's of them - my page
  will just display one of them as selected by the user - along with
  some other info) - how do I go about displaying it?
  Thanks
 
  -
  To start a new topic, e-mail: users@tomcat.apache.org
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
 
 




-- 
Kimberly Begley


Re: displaying an image from outside the webapps directory

2008-03-31 Thread Hassan Schroeder
On Sun, Mar 30, 2008 at 11:44 PM, Kimberly Begley
[EMAIL PROTECTED] wrote:

  trying to get a c:import to work - now I get the error printed in the middle
  of the page saying that:

  The requested resource (/test/home/kimberly/Desktop/testhtmlfile1.html) is
  not available

  where test is the directory under webapps and the path that was passed to
  the import statement was just /home/kimberly/Desktop...
  Any ideas how to get it to stop prefixing the path with /test?

You're giving it a path it considers context-relative -- use an absolute
url like c:import url=file:/home/kimberly/Desktop/teshtmlfile1.html/

HTH,
-- 
Hassan Schroeder  [EMAIL PROTECTED]

-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: displaying an image from outside the webapps directory

2008-03-31 Thread Christopher Schultz

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Kimberly,

Kimberly Begley wrote:
| I was hoping to use the jsp:include tag to include the snippet (no html or
| body tags) - I tried with the same code that Chris sent to open a jpeg -
| thinking it might be the same solution for the html but have gotten an
| internal error at the jsp include line.

The problem is that JSPs use HttpServletResponse.getWriter (because JSPs
are used to produce text output) while the servlet you write uses
HttpServletRequest.getOutputStream. The two are incompatible for the
same request, and getWriter has already been called, to getOutputStream
fails.

You have a couple of options:

* Write another version of OpenFile called OpenTextFile or whatever
* Modify OpenFile to try getOutputStream first, then getWriter
* Put a special flag in the request, and have OpenFile detect that flag
~  to determine whether getWriter or getOutputStream should be called

If you want to use Writers instead of Streams, you should probably open
your file using a Reader instead of a Stream as well.

Hope that helps,
- -chris

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkfw8D4ACgkQ9CaO5/Lv0PCzxwCfYMDYRCHCjLxo5bCmq+wiuvMG
0JAAn3+hBtaGqJx6Oucn/ufDBos+G6bi
=pp7j
-END PGP SIGNATURE-

-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: displaying an image from outside the webapps directory

2008-03-30 Thread Kimberly Begley
Hi,
Wasn't sure whether to start a new thread but my question is in regards to
the previous code.
As well as the jpeg I also have some html that I wanted to include in the
same page - so the html snippet is in a directory outside of the webapps
directory and when a user selects a link I'd like to include it in the web
output.
I was hoping to use the jsp:include tag to include the snippet (no html or
body tags) - I tried with the same code that Chris sent to open a jpeg -
thinking it might be the same solution for the html but have gotten an
internal error at the jsp include line. The servlet works great with a jpeg.
I've pasted the error below - I just want to check that it really would be
along the same lines to include html in a jsp as it was to include an image
(both being outside the webapps directory)
Or am I way off base?
Thanks

java.lang.IllegalStateException

org.apache.jasper.runtime.ServletResponseWrapperInclude.getOutputStream(ServletResponseWrapperInclude.java:63)
sbml.test.OpenFile.sendFile(OpenFile.java:48)
sbml.test.OpenFile.service(OpenFile.java:89)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:965)
org.apache.jsp.web.testdetails_jsp._jspService(testdetails_jsp.java:163)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


Servlet:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class OpenFile extends HttpServlet
{
private static final int BUFFER_SIZE = 4096;

protected File findFile(HttpServletRequest request)
   throws IOException
{

String plot = request.getParameter(plot);
File file = new File(plot);

return file;
}

protected String getMimeType(HttpServletRequest request, File file)
{
ServletContext application = super.getServletConfig
().getServletContext();

return application.getMimeType(file.getName());
}

protected void sendFile(File file, HttpServletResponse response)
throws IOException
{
   BufferedInputStream in = null;

try {
   int count;
byte[] buffer = new byte[BUFFER_SIZE];

in = new BufferedInputStream(new FileInputStream(file));

ServletOutputStream out = response.getOutputStream();

while(-1 != (count = in.read(buffer)))
out.write(buffer, 0, count);

out.flush();
}
finally
{
if (in != null)
{
try { in.close(); }
catch (IOException ioe) { System.err.println(IO exception
caught); }
}
}
}

public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
File file = findFile(request);

if(null == file || !file.exists())
{
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
else if(!file.canRead())
{
response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
else
{

response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(getMimeType(request, file));
response.setHeader(Content-Type,String.valueOf(file.length
()));
response.setHeader(Content-Disposition,attachment;
filename=+ file.getName());

sendFile(file, response);
}
}
}


On Tue, Mar 25, 2008 at 9:33 AM, Kimberly Begley [EMAIL PROTECTED]
wrote:

 Thanks so much!!
 The image is coming up now!
 So much appreciated!
 Kimberly


 On Tue, Mar 25, 2008 at 12:03 AM, Christopher Schultz
 [EMAIL PROTECTED] wrote:
  -BEGIN PGP SIGNED MESSAGE-
   Hash: SHA1
 
   Kimberly,
 
 
   Kimberly  wrote:
   | I might have messed it up when getting it to compile - I was having
   | problems with the getMimeType method with messages like this:
 
   Oops. It looks like I gave you some bad code, too.
 
 
   |  /usr/local/jdk1.5.0_14/bin/javac -d ../../classes/ OpenFile.java
   | OpenFile.java:35: cannot find symbol
   | symbol  : method getServletContext()
   | location: interface javax.servlet.http.HttpServletRequest
   | ServletContext application = request.getServletContext();
 
   Obviously, that should have been:
 
   ServletContext application =
   ~ super.getServletConfix().getServletContext();
 
 
   | OpenFile.java:38: incompatible types
   | found   : java.lang.String
   | required: 

Re: displaying an image from outside the webapps directory

2008-03-30 Thread Vamsavardhana Reddy
Have you considered using the allowLinking attribute (See
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html) and creating a
soft-link inside the webapp directory to the directory which is outside of
webapp?

++Vamsi

On Fri, Mar 21, 2008 at 4:28 PM, Kimberly Begley [EMAIL PROTECTED]
wrote:

 Hi,
 I'm trying to make a jsp display a plot that is a jpeg in a directory
 outside the catalina_home directory - I do not have the option of
 putting it in the webapp directory (there's 1000's of them - my page
 will just display one of them as selected by the user - along with
 some other info) - how do I go about displaying it?
 Thanks

 -
 To start a new topic, e-mail: users@tomcat.apache.org
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




Re: displaying an image from outside the webapps directory

2008-03-24 Thread Christopher Schultz

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Kimberly,

Kimberly Begley wrote:
| I might have messed it up when getting it to compile - I was having
| problems with the getMimeType method with messages like this:

Oops. It looks like I gave you some bad code, too.

|  /usr/local/jdk1.5.0_14/bin/javac -d ../../classes/ OpenFile.java
| OpenFile.java:35: cannot find symbol
| symbol  : method getServletContext()
| location: interface javax.servlet.http.HttpServletRequest
| ServletContext application = request.getServletContext();

Obviously, that should have been:

ServletContext application =
~ super.getServletConfix().getServletContext();

| OpenFile.java:38: incompatible types
| found   : java.lang.String
| required: java.io.File
| return application.getMimeType(file.getName());

And it looks like this should have been

protected String getMimeType(HttpServletRequest request, File file)
{
~  // This is a reasonable default implementation.
~  // Feel free to change it.
~  ServletContext application = request.getServletContext();

~  return application.getMimeType(file.getName());
}

...which will also fix this problem:

| OpenFile.java:92: setContentType(java.lang.String) in
| javax.servlet.ServletResponse cannot be applied to (java.io.File)
| response.setContentType(getMimeType(request, file));

Try my fixes and see what happens.

If the image does not appear, try using LiveHTTPHeaders or some other
tool to watch the HTTP request/response for the image and see what
status code is returned. Feel free to add debug logging statements to
the class to give yourself more information.

- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkfntJQACgkQ9CaO5/Lv0PBudwCgqOgUD3S9+GAePwQRAJpRNSHQ
NqgAoKuqx54kAqfzGaO+MbaWDMbySgCe
=yuDp
-END PGP SIGNATURE-

-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: displaying an image from outside the webapps directory

2008-03-24 Thread Kimberly Begley
Thanks so much!!
The image is coming up now!
So much appreciated!
Kimberly


On Tue, Mar 25, 2008 at 12:03 AM, Christopher Schultz
[EMAIL PROTECTED] wrote:
 -BEGIN PGP SIGNED MESSAGE-
  Hash: SHA1

  Kimberly,


  Kimberly Begley wrote:
  | I might have messed it up when getting it to compile - I was having
  | problems with the getMimeType method with messages like this:

  Oops. It looks like I gave you some bad code, too.


  |  /usr/local/jdk1.5.0_14/bin/javac -d ../../classes/ OpenFile.java
  | OpenFile.java:35: cannot find symbol
  | symbol  : method getServletContext()
  | location: interface javax.servlet.http.HttpServletRequest
  | ServletContext application = request.getServletContext();

  Obviously, that should have been:

  ServletContext application =
  ~ super.getServletConfix().getServletContext();


  | OpenFile.java:38: incompatible types
  | found   : java.lang.String
  | required: java.io.File
  | return application.getMimeType(file.getName());

  And it looks like this should have been

  protected String getMimeType(HttpServletRequest request, File file)

 {
  ~  // This is a reasonable default implementation.
  ~  // Feel free to change it.
  ~  ServletContext application = request.getServletContext();

  ~  return application.getMimeType(file.getName());
  }

  ...which will also fix this problem:


  | OpenFile.java:92: setContentType(java.lang.String) in
  | javax.servlet.ServletResponse cannot be applied to (java.io.File)
  | response.setContentType(getMimeType(request, file));

  Try my fixes and see what happens.

  If the image does not appear, try using LiveHTTPHeaders or some other
  tool to watch the HTTP request/response for the image and see what
  status code is returned. Feel free to add debug logging statements to
  the class to give yourself more information.


  - -chris
  -BEGIN PGP SIGNATURE-
  Version: GnuPG v1.4.8 (MingW32)
  Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

  iEYEARECAAYFAkfntJQACgkQ9CaO5/Lv0PBudwCgqOgUD3S9+GAePwQRAJpRNSHQ
  NqgAoKuqx54kAqfzGaO+MbaWDMbySgCe
  =yuDp


 -END PGP SIGNATURE-

  -
  To start a new topic, e-mail: users@tomcat.apache.org
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]





-- 
Kimberly Begley

-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: displaying an image from outside the webapps directory

2008-03-23 Thread Kimberly Begley
Thanks for that Chris - very much appreciated,
I did manage to get it to compile but when I click on the link in my
jsp page everything appears except the image - and also no errors -
when I view source it includes the img line for the plot as so:
IMG SRC=/servlet/OpenFile?plot=/home/kimberly/Desktop/test/52-plot.jpg
align=left ALT=plot

My jsp includes it as this:
IMG SRC=/servlet/OpenFile?plot=%=plot% align=left ALT=plot

Then my serlvet calls the getParameter function to get the absolute
path to the jpeg I want to display in the jsp page.

I might have messed it up when getting it to compile - I was having
problems with the getMimeType method with messages like this:
 /usr/local/jdk1.5.0_14/bin/javac -d ../../classes/ OpenFile.java
OpenFile.java:35: cannot find symbol
symbol  : method getServletContext()
location: interface javax.servlet.http.HttpServletRequest
ServletContext application = request.getServletContext();
^
OpenFile.java:38: incompatible types
found   : java.lang.String
required: java.io.File
return application.getMimeType(file.getName());
  ^
OpenFile.java:92: setContentType(java.lang.String) in
javax.servlet.ServletResponse cannot be applied to (java.io.File)
response.setContentType(getMimeType(request, file));
^
3 errors

I got rid of the first error by changing and adding these linesin the
getMimeType method:
ServletConfig config = getServletConfig();
ServletContext application = config.getServletContext();

But was still getting the other errors until I commented out the
getMimeType method and placed the above 2 lines in the else before
calling getMimeType - like this:

else
{
ServletConfig config = getServletConfig();
ServletContext application = config.getServletContext();

response.setStatus(HttpServletResponse.SC_OK);

response.setContentType(application.getMimeType(file.getName()));

response.setHeader(Content-Type,String.valueOf(file.length()));
response.setHeader(Content-Disposition,attachment;
filename=+ file.getName());

sendFile(file, response);
}

So now I'm not sure if it's a problem with the way I'm calling it or
if I've messed it up with the changes I've made to it. I've checked
the logs and nothing - no errors there either.
Thanks for all the assistance with this.
Kimberly

On Sat, Mar 22, 2008 at 12:25 AM, Christopher Schultz
[EMAIL PROTECTED] wrote:
 -BEGIN PGP SIGNED MESSAGE-
  Hash: SHA1

  Lars,


  Lars Nielsen Lind wrote:
  | You can do that with a Servlet. Here are some sample code. Replace
  | content of  with your own data.

  This would be a much better class if it:

  * Did not have extraneous class members.
  * Used a buffer for reading and writing to the streams.
  * Did not truncate content-lengths that exceed Integer.MAX_VALUE.
  * Used the proper MIME type (bug!), or allowed the servlet to
  ~  override it.
  * Handled exceptions properly.
  * Returned proper response codes.
  * Included documentation (but hey, it's sample code!)

  :(

  I point out these problems not to embarrass Lars, but to point out that
  giving code with these kinds of problems to a likely newbie (apologies,
  Kimberly, if you are not a newbie... but your question admits a certain
  level of understanding) does not serve them well. They come to the list
  hoping to get good advice from good developers and you hand them a class
  that is rife with problems.

  My guess is that this kind of thing happens all the time and the
  recipient is certainly happy to get some working code, but they don't go
  over it and fix all the problems because it came from someone smarter
  than them. Your code may become an example for how to do certain things.
  Given that, we should endeavor to provide the highest quality code
  possible to the readers of this list. We'll be helping them to be better
  programmers.

  Allow me to suggest a re-write of this class.

  package ...;


  import java.io.*;
  import java.util.*;

  import javax.servlet.*;
  import javax.servlet.http.*;

  public class DownloadServlet
  ~extends HttpServlet
  {
  ~private static final int BUFFER_SIZE = 4096;
  ~private static final String BASE_FILE_PATH = /path/to/your/files;

  ~protected File findFile(HttpServletRequest request)
  ~   throws IOException
  ~{
  ~// This is a reasonable default implementation.
  ~// Feel free to change it.
  ~File file = new File(BASE_FILE_PATH + request.getPathInfo());

  ~return file;
  ~}

  ~protected File getMimeType(HttpServletRequest request, File file)
  ~{
  ~// This is a reasonable default implementation.
  ~// Feel free to change it.
  ~ServletContext application = request.getServletContext();

  ~return 

Re: displaying an image from outside the webapps directory

2008-03-21 Thread Lars Nielsen Lind

Kimberly Begley skrev:

Hi,
I'm trying to make a jsp display a plot that is a jpeg in a directory
outside the catalina_home directory - I do not have the option of
putting it in the webapp directory (there's 1000's of them - my page
will just display one of them as selected by the user - along with
some other info) - how do I go about displaying it?
Thanks

-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


  
You can do that with a Servlet. Here are some sample code. Replace 
content of  with your own data.




package package name;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class OpenFile extends HttpServlet {
  
   private String documentHome = .;

   private String page = ;
   private String version = ;
   private String remotehost = ;
   private String remoteaddress = ;
  
   protected File findFile(HttpServletRequest request) throws IOException {

   File file = new File(filename);
  
   return file;

   }
  
   protected void sendFile(File file, HttpServletResponse response) 
throws IOException {

   int c = 0;
   FileInputStream fis = null;
  
   try {

   ServletOutputStream sos = response.getOutputStream();
   fis = new FileInputStream(file);
  
   while ((c = fis.read()) != -1) {

   sos.write(c);
   }
  
   sos.flush();

   } finally {
   if (fis != null) {
   fis.close();
   }
   }
   }
  
   public void service(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {

   ServletConfig config = getServletConfig();
   ServletContext application = config.getServletContext();
   File file = findFile(request);
  
   if (file == null) {

   } else {
  
   
response.setContentType(application.getMimeType(application/ + 
file.getName()));

   response.setContentLength((int)file.length());
   response.setHeader(Content-Disposition, attachment; 
filename =  + file.getName());
   sendFile(file, response);   
   }

   }
  
}


Hope that this is useful.

regards,

Lars Lind

-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: displaying an image from outside the webapps directory

2008-03-21 Thread Kimberly Begley
Wow - great - thanks for the fast response - I will try it out.
Have a great weekend!
Kimberly

On Fri, Mar 21, 2008 at 9:12 PM, Lars Nielsen Lind
[EMAIL PROTECTED] wrote:
 Kimberly Begley skrev:


  Hi,
   I'm trying to make a jsp display a plot that is a jpeg in a directory
   outside the catalina_home directory - I do not have the option of
   putting it in the webapp directory (there's 1000's of them - my page
   will just display one of them as selected by the user - along with
   some other info) - how do I go about displaying it?
   Thanks
  
   -
   To start a new topic, e-mail: users@tomcat.apache.org
   To unsubscribe, e-mail: [EMAIL PROTECTED]
   For additional commands, e-mail: [EMAIL PROTECTED]
  
  
  
  You can do that with a Servlet. Here are some sample code. Replace
  content of  with your own data.



  package package name;

  import java.io.*;
  import java.util.*;

  import javax.servlet.*;
  import javax.servlet.http.*;

  public class OpenFile extends HttpServlet {

 private String documentHome = .;
 private String page = ;
 private String version = ;
 private String remotehost = ;
 private String remoteaddress = ;

 protected File findFile(HttpServletRequest request) throws IOException {
 File file = new File(filename);

 return file;
 }

 protected void sendFile(File file, HttpServletResponse response)
  throws IOException {
 int c = 0;
 FileInputStream fis = null;

 try {
 ServletOutputStream sos = response.getOutputStream();
 fis = new FileInputStream(file);

 while ((c = fis.read()) != -1) {
 sos.write(c);
 }

 sos.flush();
 } finally {
 if (fis != null) {
 fis.close();
 }
 }
 }

 public void service(HttpServletRequest request, HttpServletResponse
  response) throws ServletException, IOException {
 ServletConfig config = getServletConfig();
 ServletContext application = config.getServletContext();
 File file = findFile(request);

 if (file == null) {
 } else {


  response.setContentType(application.getMimeType(application/ +
  file.getName()));
 response.setContentLength((int)file.length());
 response.setHeader(Content-Disposition, attachment;
  filename =  + file.getName());
 sendFile(file, response);
 }
 }

  }

  Hope that this is useful.

  regards,

  Lars Lind

  -
  To start a new topic, e-mail: users@tomcat.apache.org
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]





-- 
Kimberly Begley

-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: displaying an image from outside the webapps directory

2008-03-21 Thread Christopher Schultz

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Lars,

Lars Nielsen Lind wrote:
| You can do that with a Servlet. Here are some sample code. Replace
| content of  with your own data.

This would be a much better class if it:

* Did not have extraneous class members.
* Used a buffer for reading and writing to the streams.
* Did not truncate content-lengths that exceed Integer.MAX_VALUE.
* Used the proper MIME type (bug!), or allowed the servlet to
~  override it.
* Handled exceptions properly.
* Returned proper response codes.
* Included documentation (but hey, it's sample code!)

:(

I point out these problems not to embarrass Lars, but to point out that
giving code with these kinds of problems to a likely newbie (apologies,
Kimberly, if you are not a newbie... but your question admits a certain
level of understanding) does not serve them well. They come to the list
hoping to get good advice from good developers and you hand them a class
that is rife with problems.

My guess is that this kind of thing happens all the time and the
recipient is certainly happy to get some working code, but they don't go
over it and fix all the problems because it came from someone smarter
than them. Your code may become an example for how to do certain things.
Given that, we should endeavor to provide the highest quality code
possible to the readers of this list. We'll be helping them to be better
programmers.

Allow me to suggest a re-write of this class.

package ...;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class DownloadServlet
~extends HttpServlet
{
~private static final int BUFFER_SIZE = 4096;
~private static final String BASE_FILE_PATH = /path/to/your/files;

~protected File findFile(HttpServletRequest request)
~   throws IOException
~{
~// This is a reasonable default implementation.
~// Feel free to change it.
~File file = new File(BASE_FILE_PATH + request.getPathInfo());

~return file;
~}

~protected File getMimeType(HttpServletRequest request, File file)
~{
~// This is a reasonable default implementation.
~// Feel free to change it.
~ServletContext application = request.getServletContext();

~return application.getMimeType(file.getName());
~}

~protected void sendFile(File file, HttpServletResponse response)
~throws IOException
~{
BufferedInputStream in = null;

~try {
int count;
~byte[] buffer = new byte[BUFFER_SIZE];

~in = new BufferedInputStream(new FileInputStream(file));

~ServletOutputStream out = response.getOutputStream();

~while(-1 != (count = in.read(buffer)))
~out.write(buffer, 0, count);

~out.flush();
~}
~finally
~{
~if (in != null)
~{
~try { in.close(); }
~catch (IOException ioe) { /* log an error! */ }
~}
~}
~}

~public void service(HttpServletRequest request,
~HttpServletResponse response)
~throws ServletException, IOException
~{
~File file = findFile(request);

~if(null == file || !file.exists())
~{
~response.sendError(HttpServletResponse.SC_NOT_FOUND);
~}
~else if(!file.canRead())
~{
~// Feel free to send NOT_FOUND instead, if you don't want to
~// give up potentially sensitive security information.
~response.sendError(HttpServletResponse.SC_FORBIDDEN);
~}
~else
~{
~response.setStatus(HttpServletResponse.SC_OK);
~response.setContentType(getMimeType(request, file));
~response.setHeader(Content-Type,
~   String.valueOf(file.length());
~response.setHeader(Content-Disposition,
~   attachment; filename=
~  + file.getName());

~sendFile(file, response);
~}
~}
}

- -chris

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkfjxWoACgkQ9CaO5/Lv0PBiFACeJuF3Gb91N5pwJlaWDFrVkksb
NhYAoKg9CtqkwNqmO2l0iA8+pPBzPM1d
=1LA6
-END PGP SIGNATURE-

-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]