《基于httpcorehttpclientcomponent搭建轻量级http服务器》由会员分享,可在线阅读,更多相关《基于httpcorehttpclientcomponent搭建轻量级http服务器(13页珍藏版)》请在金锄头文库上搜索。
1、基于httpcore(httpclient component)搭建轻量级http服务器 作者: 日期:基于httpcore(httpclient component)搭建轻量级http服务器下面是apache官网例子服务器端接受请求,实现接收文件请求处理器import java.io.File;import java.io.IOException;import java.io.InterruptedIOException;import .ServerSocket;import .Socket;import .URL;import .URLDecoder;import java.nio.cha
2、rset.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.
3、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.Strin
4、gEntity;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.
5、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.Respons
6、eServer;import org.apache.http.protocol.UriHttpRequestHandlerMapper;import org.apache.http.util.EntityUtils;import .ssl.KeyManager;import .ssl.KeyManagerFactory;import .ssl.SSLContext;import .ssl.SSLServerSocketFactory;/* * Basic, yet fully functional and spec compliant, HTTP/1.1 file server. */publ
7、ic 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 Respo
8、nseContent() .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); SSLServ
9、erSocketFactory 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.loa
10、d(url.openStream(), secret.toCharArray(); KeyManagerFactory kmfactory = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm(); kmfactory.init(keystore, secret.toCharArray(); KeyManager keymanagers = kmfactory.getKeyManagers(); SSLContext sslcontext = SSLContext.getInstance(TLS); ssl
11、context.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 do
12、cRoot) 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);