Существует Tomcat 8 (установленный на порт 9001) и контекст контекста сервлета, а имя проекта - «ROOT», поэтому каждый запрос, такой как: http://myserver:9001/smthng/ ... должен быть получен Servlet.java Servlet proxy не загружает некоторые файлы PDF
Клиенты (конечные пользователи) устанавливают свои браузерные прокси как http://myserver:9001, поэтому мой сервлет получает запрос от клиента и использует другой прокси для отправки нового запроса и получения ответа, а затем отправки ответа клиенту.
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 Web страницы,