title: java Servlet proxy does not download some pdf files
body: <p>There is Tomcat 8 (set on port 9001) and a Servlet whit context path / and project name is "ROOT" so every request like :  <a href="http://myserver:9001/smthng/" rel="nofollow">http://myserver:9001/smthng/</a>... must received by Servlet.</p>
  
  <p>Clients (end users) set their browsers proxies as <a href="http://myserver:9001" rel="nofollow">http://myserver:9001</a>, So my Servlet get request from client and use another proxy to send new request and get response and then send response to client.</p>
  
  <pre><code>    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      System.out.println("##### header(host) ##### " + request.getHeader("host"));
      System.out.println("##### getRemoteAddr ##### " + request.getRemoteAddr());
      System.out.println("##### request.getScheme() ##### " + request.getScheme());
      String proxyAdress = "Proxy.haccettepe.edu.tr";
      String proxyPort = "8080";
      System.setProperty("http.proxyHost", proxyAdress);
      System.setProperty("http.proxyPort", proxyPort);
  
      Authenticator authenticator = new Authenticator() {
          public PasswordAuthentication getPasswordAuthentication() {
              System.out.println("$$$$$$$$$$$$$$$$$$$$$$$");
              return (new PasswordAuthentication("yildirims",
                      "9891".toCharArray()));
          }
      };
      System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%% " + authenticator.toString());
      Authenticator.setDefault(authenticator);
      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("Proxy.haccettepe.edu.tr", 8080));
  
      ///////----&gt;HttpURLConnection with proxy
      URL url = new URL((request.getScheme() + "://" + request.getHeader("host") + request.getRequestURI() + "?" + request.getQueryString()).replace("?null", ""));
      System.out.println("URL ============= " + url);
  
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
      conn.setRequestMethod(request.getMethod());
      System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;" + conn.usingProxy());
      conn.connect();
      InputStream is2 = conn.getInputStream();
      System.out.println("content type=&gt;&gt;&gt;&gt;&gt;&gt;&gt;"+ conn.getContentType());
      System.out.println("content getContentEncoding=&gt;&gt;&gt;&gt;&gt;&gt;&gt;"+ conn.getContentEncoding());
      System.out.println("content getHeaderFieldKey=&gt;&gt;&gt;&gt;&gt;&gt;&gt;"+ conn.getHeaderFieldKey(0));
      System.out.println("content getRequestMethod=&gt;&gt;&gt;&gt;&gt;&gt;&gt;"+ conn.getRequestMethod());
      HttpServletResponse rs=new HttpServletResponseWrapper(response);
  
      OutputStream oos = response.getOutputStream();
          response.setHeader("Content-type", conn.getContentType());
          response.setContentType(conn.getContentType());
      byte[] buf = new byte[102400];
      int c = 0;
      while ((c = is2.read(buf, 0, buf.length)) &gt; 0) {       
          oos.write(buf, 0, c);
          if (url.toString().endsWith(".pdf")) {
              oos.flush();
          }
      }
      if (url.toString().endsWith(".pdf")) {
          oos.close();
          is2.close();
          }
      }
  </code></pre>
  
  <p>so this code work for some simple pages and download some simple .pdf ,  but not work for some .pdf files like this : </p>
  
  <p><a href="http://link.springer.com/content/pdf/10.1007%2Fs00128-015-1658-6.pdf" rel="nofollow">http://link.springer.com/content/pdf/10.1007%2Fs00128-015-1658-6.pdf</a></p>
  
  <p>The code below works for many http web pages ,</p>