title: Can GWT's RemoteServiceServlet write a file stream in HttpServletResponse?
body: <p>I have a class that extends RemoteServiceServlet. This class has several methods, in one method, I use getThreadLocalResponse() to get the HttpServletResponse  for the current call, and then write a File in the response. Here is the code:</p>
  
  <pre><code>File aFile = new File("c://test.txt");
  int iBufferSize = 1000;
  int iLength = 0;
  
  HttpServletResponse resp = getThreadLocalResponse();
  
  ServletOutputStream op = resp.getOutputStream();
  ServletContext context = getServletConfig().getServletContext();
  String mimetype = context.getMimeType(aFile.getName());
  
  resp.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
  resp.setContentLength((int) aFile.length());
  resp.setHeader("Content-Disposition", "attachment; filename=\"" + aFile.getName() + "\"");
  
  byte[] xbuf = new byte[iBufferSize];
  DataInputStream in = new DataInputStream(new FileInputStream(aFile));
  
  while ((in != null) &amp;&amp; ((iLength = in.read(xbuf)) != -1))
  {
    op.write(xbuf, 0, iLength);
  }
  
  in.close();
  op.flush();
  op.close();
  </code></pre>
  
  <p>However, an error always occurs. After debugging, I find out that an Exception is thrown when it writes the response.</p>
  
  <p>I don't override doGet and doPost, because there are some other methods, I don't want every request to the class can invoke this piece of code.</p>
  
  <p>But if I create a separate Servlet or override doGet or doPost in this class, it works fine. </p>
  
  <p>Does anybody know why? Does GWT RemoteServiceServlet support writing a Stream in the response when we use getThreadLocalResponse() ?</p>
  
  <p>Thanks!</p>