资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
基于httpcore(httpclient component)搭建轻量级http服务器 作者: 日期:基于httpcore(httpclient component)搭建轻量级http服务器下面是apache官网例子服务器端接受请求,实现接收文件请求处理器import java.io.File;import java.io.IOException;import java.io.InterruptedIOException;import java.net.ServerSocket;import java.net.Socket;import java.net.URL;import java.net.URLDecoder;import java.nio.charset.Charset;import java.security.KeyStore;import java.util.Locale;import org.apache.http.ConnectionClosedException;import org.apache.http.HttpConnectionFactory;import org.apache.http.HttpEntity;import org.apache.http.HttpEntityEnclosingRequest;import org.apache.http.HttpException;import org.apache.http.HttpRequest;import org.apache.http.HttpResponse;import org.apache.http.HttpServerConnection;import org.apache.http.HttpStatus;import org.apache.http.MethodNotSupportedException;import org.apache.http.entity.ContentType;import org.apache.http.entity.FileEntity;import org.apache.http.entity.StringEntity;import org.apache.http.impl.DefaultBHttpServerConnection;import org.apache.http.impl.DefaultBHttpServerConnectionFactory;import org.apache.http.protocol.BasicHttpContext;import org.apache.http.protocol.HttpContext;import org.apache.http.protocol.HttpProcessor;import org.apache.http.protocol.HttpProcessorBuilder;import org.apache.http.protocol.HttpRequestHandler;import org.apache.http.protocol.HttpService;import org.apache.http.protocol.ResponseConnControl;import org.apache.http.protocol.ResponseContent;import org.apache.http.protocol.ResponseDate;import org.apache.http.protocol.ResponseServer;import org.apache.http.protocol.UriHttpRequestHandlerMapper;import org.apache.http.util.EntityUtils;import javax.net.ssl.KeyManager;import javax.net.ssl.KeyManagerFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLServerSocketFactory;/* * Basic, yet fully functional and spec compliant, HTTP/1.1 file server. */public class ElementalHttpServer public static void main(String args) throws Exception if (args.length = 2) port = Integer.parseInt(args1); / Set up the HTTP protocol processor HttpProcessor httpproc = HttpProcessorBuilder.create() .add(new ResponseDate() .add(new ResponseServer(Test/1.1) .add(new ResponseContent() .add(new ResponseConnControl().build(); / Set up request handlers UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper(); reqistry.register(*, new HttpFileHandler(docRoot); / Set up the HTTP service HttpService httpService = new HttpService(httpproc, reqistry); SSLServerSocketFactory sf = null; if (port = 8443) / Initialize SSL context ClassLoader cl = ElementalHttpServer.class.getClassLoader(); URL url = cl.getResource(my.keystore); if (url = null) System.out.println(Keystore not found); System.exit(1); KeyStore keystore = KeyStore.getInstance(jks); keystore.load(url.openStream(), secret.toCharArray(); KeyManagerFactory kmfactory = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm(); kmfactory.init(keystore, secret.toCharArray(); KeyManager keymanagers = kmfactory.getKeyManagers(); SSLContext sslcontext = SSLContext.getInstance(TLS); sslcontext.init(keymanagers, null, null); sf = sslcontext.getServerSocketFactory(); Thread t = new RequestListenerThread(port, httpService, sf); t.setDaemon(false); t.start(); static class HttpFileHandler implements HttpRequestHandler private final String docRoot; public HttpFileHandler(final String docRoot) super(); this.docRoot = docRoot; public void handle( final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); if (!method.equals(GET) & !method.equals(HEAD) & !method.equals(POST) throw new MethodNotSupportedException(method + method not supported);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号