Skip to content

Commit

Permalink
演示使用HttpUrlConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Mar 28, 2012
1 parent ffbfa8b commit 158c7e3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.springside.examples.showcase.web;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
Expand Down Expand Up @@ -78,8 +81,38 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "contentUrl parameter is required.");
}

//远程访问获取内容
HttpEntity entity = fetchContent(contentUrl);
//远程访问获取内容的方式
String client = request.getParameter("client");

InputStream input = null;
if ("apache".equals(client)) {
//使用Apache HttpClient
fetchContentByApacheHttpClient(response, contentUrl);
} else {
//使用JDK HttpUrlConnection
fetchContentByJDKConnection(response, contentUrl);
}
}

/**
* 使用HttpClient取得内容.
*/
private void fetchContentByApacheHttpClient(HttpServletResponse response, String contentUrl) throws IOException {

//获取内容
HttpEntity entity = null;
HttpGet httpGet = new HttpGet(contentUrl);
try {
HttpContext context = new BasicHttpContext();
HttpResponse remoteResponse = httpClient.execute(httpGet, context);
entity = remoteResponse.getEntity();
} catch (Exception e) {
logger.error("fetch remote content" + contentUrl + " error", e);
httpGet.abort();
return;
}

//404返回
if (entity == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, contentUrl + " is not found.");
return;
Expand All @@ -94,7 +127,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
//输出内容
InputStream input = entity.getContent();
OutputStream output = response.getOutputStream();

try {
//基于byte数组读取InputStream并直接写入OutputStream, 数组默认大小为4k.
IOUtils.copy(input, output);
Expand All @@ -103,22 +135,44 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
//保证InputStream的关闭.
IOUtils.closeQuietly(input);
}

}

/**
* 使用HttpClient取得内容.
*/
private HttpEntity fetchContent(String targetUrl) {
HttpGet httpGet = new HttpGet(targetUrl);
HttpContext context = new BasicHttpContext();
private void fetchContentByJDKConnection(HttpServletResponse response, String contentUrl) throws IOException {

HttpURLConnection connection = (HttpURLConnection) new URL(contentUrl).openConnection();
//设置Socket超时
connection.setReadTimeout(TIMEOUT_SECONDS * 1000);
try {
HttpResponse remoteResponse = httpClient.execute(httpGet, context);
return remoteResponse.getEntity();
} catch (Exception e) {
logger.error("fetch remote content" + targetUrl + " error", e);
httpGet.abort();
return null;
connection.connect();

//真正发出请求
InputStream input;
try {
input = connection.getInputStream();
} catch (FileNotFoundException e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, contentUrl + " is not found.");
return;
}

//设置Header
response.setContentType(connection.getContentType());
if (connection.getContentLength() > 0) {
response.setContentLength(connection.getContentLength());
}

//输出内容
OutputStream output = response.getOutputStream();
try {
//基于byte数组读取InputStream并直接写入OutputStream, 数组默认大小为4k.
IOUtils.copy(input, output);
output.flush();
} finally {
//保证InputStream的关闭.
IOUtils.closeQuietly(input);
}
} finally {
connection.disconnect();
}
}

}
5 changes: 3 additions & 2 deletions examples/showcase/src/main/webapp/WEB-INF/views/story/web.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
<p>
1. 静态内容Servlet, 演示高效读取静态内容, 控制客户端缓存, 压缩传输, 弹出下载对话框, 见StaticContentServlet.<br/>
<img src="${ctx}/static-content?contentPath=static/img/logo.jpg"/> <a href="${ctx}/static-content?contentPath=img/logo.jpg&download=true">图片下载链接</a><br/>
2. 远程内容Servlet, 演示使用Apache HttpClient多线程高效获取远程网站内容, 见RemoteContentServlet.<br/>
<img src="${ctx}/remote-content?contentUrl=<%=encodedImageUrl%>"/><br/>
2. 远程内容Servlet, 演示使用两种http client多线程高效获取远程网站内容, 见RemoteContentServlet.<br/>
<img src="${ctx}/remote-content?client=apache&contentUrl=<%=encodedImageUrl%>"/> Apache HttpClient.<br/>
<img src="${ctx}/remote-content?client=jdk&contentUrl=<%=encodedImageUrl%>"/> JDK HttpUrlConnection.<br/>
3. CacheControlHeaderFilter为静态内容添加缓存控制 Header.<br/>
4. YUI Compressor 压缩js/css,见bin/yuicompressor.bat命令及webapp中两个版本的js/css文件<br/>
</p>
Expand Down

0 comments on commit 158c7e3

Please sign in to comment.