aspose file tools*
The moose likes Servlets and the fly likes Servlet download file writer is already used by this request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet download file writer is already used by this request" Watch "Servlet download file writer is already used by this request" New topic
Author

Servlet download file writer is already used by this request

Karen Fishwick
Greenhorn

Joined: Aug 23, 2001
Posts: 2
Hello,
I am a beginner jsp/java developer. My problem seems very common, but I have tried many posted answers and can't seem to solve it. I am using a servlet to try to download a file to the client. I want this file to download as an attachment.
I get an error "java.lang.IllegalStateException: Writer is already being used for this request". I have tried using a PrintWriter, Writer, and OutputStream types to download the file.
I have an additional question which is, how do you set a default location for the file to be saved on the client (I would like to save straight to diskette).
Here is my servlet code:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/x-filler");
String fileName = "c:/jakarta-tomcat/smithapp/01/batches.txt";
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
FileInputStream fileInput = new FileInputStream(fileName);
int numOfBytes = fileInput.available();
byte byteArray[] = new byte[numOfBytes];
int nextByte = fileInput.read(byteArray);
fileInput.close();
OutputStream outStream = response.getOutputStream();
outStream.write(byteArray);
outStream.close();
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

The only thing I could see without trying the code was that getOutputStream actually returns a ServletOutputStream - I don't know if using its superclass would be a problem here or not.
As far as local file names go, this depends on what method of file-saving you're doing. Java applets are constrained by their sandboxes. JavaScript allows you to specify the default filename for a "save" dialog, I believe.


Customer surveys are for companies who didn't pay proper attention to begin with.
Karen Fishwick
Greenhorn

Joined: Aug 23, 2001
Posts: 2
Tim,
I think my problem was related to the way I was calling the servlet (by jsp:include or jsp:forward). I changed it to forward to the url after a short timeout and that seemed to fix things.
I can get the filename to default but I have trouble with the drive. It might just be that I'm not escaping my characters correctly, it appears to ignore the 'a:\download.txt' and replaces it with the url. If I take off the a:\ then 'download.txt' is the default file.
Thanks!
Karen
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
JSP's cannot write binary data to the response output stream, either directly or by calling <jsp:include>. The JSP specification explicitly forbids it. The reason for this is that the JSP container automatically initializes the "out" implicit variable with response.getWriter(). If you look at the API documentation for javax.servlet.ServletResponse, you'll note that you can call either getWriter() or getOutputStream(), but not both.
Tomcat defers opening the JspWriter "out" object until you actually write a character, but it will still probably throw an IllegalStateException when it does.
You can write a binary output stream from a servlet, exactly as you have described, but not from a JSP (at least not reliably). If you need to use the JSP as the front end to the application, then use response.sendRedirect("the servlet URL") instead of <jsp:forward>. That will cause the client browser to initiate an additional request, which will not comingle the character and binary output.
------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
[This message has been edited by Phil Hanna (edited August 25, 2001).]


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
 
 
subject: Servlet download file writer is already used by this request