java-模拟tomcat服务器

来源:互联网 发布:阿里云校园 编辑:程序博客网 时间:2024/05/02 02:14
模拟tomcat服务器端代码示例:
package cd.itcast.day5;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.Date;public class HttpServerDemo {public static void main(String[] args) {(new HttpServer()).run();}public static class HttpServer {public void run() {String filePath = "D:\\Java\\apache-tomcat-6.0.35\\webapps\\ROOT";ServerSocket serverSocket = null;try {serverSocket = new ServerSocket(8888);// 开启线程,接收客户端请求.while (true) {Socket socket = serverSocket.accept();Thread thread = new Thread(new MyHttpServerThread(socket,filePath));thread.start();}} catch (IOException e2) {e2.printStackTrace();}}}public static class MyHttpServerThread implements Runnable {String filePath = null;private Socket socket = null;private File file = null;public MyHttpServerThread(Socket socket, String filePath) {this.filePath = filePath;this.socket = socket;}@Overridepublic void run() {// 读取客户端数据BufferedReader request = null;// 处理客户端数据并返回.BufferedOutputStream response = null;try {// System.out.println(socket.getInetAddress().getHostAddress()// + " 连接到服务器.");InputStream inputStream = socket.getInputStream();OutputStream outputStream = socket.getOutputStream();request = new BufferedReader(new InputStreamReader(inputStream));response = new BufferedOutputStream(outputStream);// 读取Http头信息行:GET /index.html HTTP/1.1String httpHead = request.readLine();if (httpHead != null) {String[] arr = httpHead.split(" ");if (arr.length == 3&& ("GET".equals(arr[0]) || "POST".equals(arr[0]))&& "HTTP/1.1".equals(arr[2])) {// 获取要访问的文件.if ("/".equals(arr[2])) {file = new File(filePath, "/index.html");} else {file = new File(filePath, arr[1]);}if (file.exists() && file.isFile()) {responseRequest(response);} else {System.out.println("请求的页面:" + arr[1] + " 不存在!");}} else {System.out.println("客户端请求非法:" + arr[0]);}} else {System.out.println("未发现客户端请求或请求有误!");}} catch (Exception e) {e.printStackTrace();} finally {if (request != null) {try {request.close();} catch (IOException e1) {e1.printStackTrace();}}if (response != null) {try {response.close();} catch (IOException e) {e.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}}private void responseRequest(BufferedOutputStream response)throws IOException, FileNotFoundException {// 返回给客户端服务器的Http头信息response.write("HTTP/1.1 200 ok\r\n".getBytes());response.write("Server: HttpTest Server/1.1\r\n".getBytes());response.write("Content-Type: text/html\r\n".getBytes());response.write(("Content-Length: 5866" + file.length() + "\r\n").getBytes());response.write(("Date: " + new Date() + "\r\n").getBytes());// 与实体部分以空行隔开.response.write("\r\n".getBytes());FileInputStream fis = new FileInputStream(file);byte[] buff = new byte[1024];int len = 0;while ((len = fis.read(buff)) != -1) {response.write(buff, 0, len);response.flush();}fis.close();}}}


客户端http请求模拟示例:
package cd.itcast.day5;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.Socket;import java.net.UnknownHostException;public class HttpClientDemo {public static void main(String[] args) {BufferedWriter out = null;BufferedReader in = null;try {Socket socket = new Socket("127.0.0.1", 8888);out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//发送Http头信息到服务器.请求页面为httptest.html.out.write("GET /httptest.html HTTP/1.1\r\n");out.write("Host: localhost\r\n");out.newLine();out.flush();//获取服务器返回内容.String line = null;while ((line = in.readLine()) != null) {System.out.println(line);}} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}if (out != null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}}}