>
> I have 3 problems:
> a) I have the file name in the servlet, but I don't know how to send it to
> the browser.
>
Here's a class I use in my web application to send a file to the
client/browser:
public class FileHandler extends ResponseHandler {
public void execute() throws IOException, ServletException {
UploadedFile file = (UploadedFile)responseObject;
ServletOutputStream out = response.getOutputStream();
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(
file.getPath()
),
1024
);
response.setHeader("Content-Disposition", "inline; filename=" +
file.getRealFilename());
response.setContentType("application/octet-stream");
response.setContentLength((int)file.getSize());
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.flush();
out.close();
in.close();
}
}
UploadedFile is a bean class which contains the information about the file
that should be sent, like its path and the file's name. You should be able
to
copy the code your need from there.
>
> b) If the browser recognizes the file (ie txt, gif or html): it
> displays it
> on the current browser window.(something I want to avoid)
>
I don't think there's much you can do about this, unless the user
specifically
right-clicks on the link and choose "Save target as..." or something.
>
> c) If the browser doesn't recognize the file (ie zip, xls...), it
> opens the
> dialog to save it. If the user chooses "save the file in disk",
> it gives as
> default name "programad63e5b8d.txt" (servlet's name the links point to is
> called "programa"). If the user chooses "open from its current
> location", it
> opens it with notepad.
>
>
See my code above to see how to set the default filename.
[ Matthias Carlsson ]
[ Programmer (Java, XML/XSL, CGI/Perl, HTML/JS) ] [ Web Designer ]
[ E-Mail : [EMAIL PROTECTED] ] [ ICQ: 1430647 ]
___________________________________________________________________________
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