java Servlet代理不下载一些pdf文件

时间:2016-10-08 08:52:53

标签: java tomcat pdf servlets proxy

Tomcat 8(在端口9001上设置)和Servlet whit上下文路径/项目名称为“ROOT”,因此每个请求都必须:http://myserver:9001/smthng/ ...必须由Servlet接收。

客户端(最终用户)将其浏览器代理设置为http://myserver:9001,因此我的Servlet从客户端获取请求并使用另一个代理发送新请求并获得响应,然后将响应发送给客户端。

    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));

    ///////---->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(">>>>>>>>>>>>>>>>>>>" + conn.usingProxy());
    conn.connect();
    InputStream is2 = conn.getInputStream();
    System.out.println("content type=>>>>>>>"+ conn.getContentType());
    System.out.println("content getContentEncoding=>>>>>>>"+ conn.getContentEncoding());
    System.out.println("content getHeaderFieldKey=>>>>>>>"+ conn.getHeaderFieldKey(0));
    System.out.println("content getRequestMethod=>>>>>>>"+ 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)) > 0) {       
        oos.write(buf, 0, c);
        if (url.toString().endsWith(".pdf")) {
            oos.flush();
        }
    }
    if (url.toString().endsWith(".pdf")) {
        oos.close();
        is2.close();
        }
    }

所以这段代码适用于一些简单的页面并下载一些简单的.pdf,但不适用于某些.pdf文件,如下所示:

http://link.springer.com/content/pdf/10.1007%2Fs00128-015-1658-6.pdf

以下代码适用于许多http网页,

1 个答案:

答案 0 :(得分:0)

某个网址有/所以配置tomcat不拒绝它们

某些请求已发布,有些cokies需要下载。