I have a servlet who's purpose is to deliver files in a members-only area to our
customers. The routine that delivers files works fine by itself, when the routine is
in its own servlet.
However, since I need to control access to these files via an authentication process,
I put the routine into an existing servlet, and call the routine based upon a request
parameter and the verification of user login.
What I get is an IllegalStateException when I attempt to stream the file out to the
customer at the following line:
ServletOutputStream out = resp.getOutputStream();
If I happen to run this under servletrunner, the more verbose output is that I can't
mix text and binary input.
Setting the HttpServletResponse object to null first sure doesn't work. A quick
toString() of the response object gives me a hex number which is apparently the object
reference (not useful).
Prior to sending the file, the only response to the client would have been the reading
or setting of a cookie. I also set the content-type so as to allow the browser to open
the file automatically if possible. Any ideas?
Below is the method I used:
----------------------------------------------------
public void sendFile(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
String file = req.getParameter("file");
log("File to Download is " + root + file);
int filename_length = file.length();
int filename_extensionspot = file.indexOf(".");
String filename_extension = file.substring(filename_extensionspot + 1,
filename_length);
String FileLocation = "";
if (filename_extension.compareTo("pdf") == 0){
res.setContentType("application/pdf");
FileLocation = root + "pdf/";
} else if (filename_extension.compareTo("ra") == 0 ||
filename_extension.compareTo("ram") == 0){
res.setContentType("audio/x-pn-realaudio");
FileLocation = root + "audio/";
}
ServletOutputStream out = res.getOutputStream();
InputStream in = null;
try {
in = new BufferedInputStream( new FileInputStream(FileLocation + file) );
int ch;
while ((ch = in.read()) !=-1) {
out.print((char)ch);
}
} catch (IOException ioe) {
// whatever
}
finally {
if (in != null) in.close();
}
}
-------------------------------------------------
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html