• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet download file writer is already used by this request

 
Karen Fishwick
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karen Fishwick
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic