【发布时间】:2011-06-08 14:44:51
【问题描述】:
目前我正在使用 HttpURLConnection 加载远程网页并呈现给我的客户(使用 InputStream 到 HttpResponse 的 outputStream 传输),它可以正确加载 html 但会跳过图像,如何解决?
谢谢
【问题讨论】:
标签: java servlets httpurlconnection
目前我正在使用 HttpURLConnection 加载远程网页并呈现给我的客户(使用 InputStream 到 HttpResponse 的 outputStream 传输),它可以正确加载 html 但会跳过图像,如何解决?
谢谢
【问题讨论】:
标签: java servlets httpurlconnection
您需要以这种方式操作 HTML,以便 Intranet 域上的所有资源 URL 也被代理。例如。以下所有 HTML 中的资源引用
<base href="http://intranet.com/" />
<script src="http://intranet.com/script.js"></script>
<link href="http://intranet.com/style.css" />
<img src="http://intranet.com/image.png" />
<a href="http://intranet.com/page.html">link</a>
应该以这种方式在 HTML 中进行更改,以便它们通过您的代理 servlet,例如
<base href="http://example.com/proxy/" />
<script src="http://example.com/proxy/script.js"></script>
<link href="http://example.com/proxy/style.css" />
<img src="http://example.com/proxy/image.png" />
<a href="http://example.com/proxy/page.html">link</a>
像Jsoup 这样的HTML 解析器在这方面非常有帮助。您可以在您的代理 servlet 中执行以下操作,我假设它映射到 /proxy/* 的 URL 模式。
String intranetURL = "http://intranet.com";
String internetURL = "http://example.com/proxy";
if (request.getRequestURI().endsWith(".html")) { // A HTML page is requested.
Document document = Jsoup.connect(intranetURL + request.getPathInfo()).get();
for (Element element : document.select("[href]")) {
element.attr("href", element.absUrl("href").replaceFirst(intranetURL, internetURL));
}
for (Element element : document.select("[src]")) {
element.attr("src", element.absUrl("src").replaceFirst(intranetURL, internetURL));
}
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
resposne.getWriter().write(document.html());
}
else { // Other resources like images, etc.
URLConnection connection = new URL(intranetURL + request.getPathInfo()).openConnection();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
for (String value : header.getValue()) {
response.addHeader(header.getKey(), value);
}
}
InputStream input = connection.getInputStream();
OutputStream output = response.getOutputStream();
// Now just copy input to output.
}
【讨论】:
您必须为每张图片单独提出请求。这也是浏览器所做的。
【讨论】: