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: 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: [email protected]
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
>
>
> --
>
>