title: Content-Disposition attachment doesn't work - prints bits to screen
body: <p>I'm trying to download a PDF file in Struts Action class.
  Problem is that using</p>
  
  <pre><code>response.setHeader("Content-Disposition", "attachment;filename=file.pdf");
  </code></pre>
  
  <p>I want to open box for "save/open", but now PDF content is writen in browser:
  ex.</p>
  
  <pre><code>%PDF-1.4 28 0 obj &lt;&lt; /Type /XObject /Subtype /Image /Filter /DCTDecode /Length 7746 /Width 200 /Height 123 /BitsPerComponent 8 /ColorSpace /DeviceRGB &gt;&gt;...(cut)
  </code></pre>
  
  <p>I was trying this code (below) under Chrome, Firefox and IE and everywhere this same. Also I was using different PDF files for that. </p>
  
  <p>My code fragment:</p>
  
  <pre><code>try {
      URL fileUrl = new URL("file:///" + filePath);
      URLConnection connection = fileUrl.openConnection();
      inputStream = connection.getInputStream();
      int fileLength = connection.getContentLength();
      byte[] outputStreamBytes = new byte[100000];
      response.setContentType("application/pdf");
      response.setHeader("Content-Disposition", "attachment;filename=file.pdf");
      response.setContentLength(fileLength);
      outputStream = response.getOutputStream();
      int iR;
      while ((iR = inputStream.read(outputStreamBytes)) &gt; 0) {
          outputStream.write(outputStreamBytes, 0, iR);
      }
  
      return null;
  } catch (MalformedURLException e) {
      logger.debug("service", "An error occured while creating URL object for url: "
          + filePath);
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return null;
  } catch (IOException e) {
      logger.debug("service", "An error occured while opening connection for url: "
          + filePath);
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return null;
  } finally {
      if (outputStream != null) {
          outputStream.close();
      }
      if (inputStream != null) {
          inputStream.close();
      }
      inputStream.close();
  }
  return null;
  </code></pre>
  
  <p>Is something still missing?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>When I use this code in the Struts class it does not work, but when I use this code in the Servlet it is working.
  The strangest thing is that when I in action class write only "response.sendRedirect()" to Servlet (and all logic is in Servlet) it is not working too.</p>
  
  <p>When I analyzed the response headers everything in these three examples is the same.</p>