- 建立一个Socket并用死循环的形式监听指定端口
package com.study.diyserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
public static String ROOT = "src/main/resources";
public static String defaultPage = "index.html";
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8000);
while (true) {
Socket sk = server.accept();
System.out.println("等待连接...");
new HttpThread(sk).start();
}
}
}
- 实现一个多线程的程序,用来处理每一个用户的请求
package com.study.diyserver;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class HttpThread extends Thread {
private Socket socket;
public HttpThread(Socket sk) {
this.socket = sk;
}
@Override
public void run() {
InputStream ins = null;
OutputStream outs = null;
try {
ins = socket.getInputStream();
outs = socket.getOutputStream();
Receive receive = new Receive(ins);
String URL = receive.parse();
System.out.println("URL:"+URL);
if ("/".equals(URL)) {
URL = HttpServer.defaultPage;
}
System.out.println("URL:"+URL);
Answer ans = new Answer(outs);
ans.Send(URL);
} catch (IOException e) {
System.out.println(e.toString());
}finally{
try {
if (ins != null) {
ins.close();
}
if (outs != null) {
outs.close();
}
if (socket != null) {
socket.close();
}
} catch (Exception e){} {
}
}
}
}
- 本类取得了浏览器传过来的URL字符串
package com.study.diyserver;
import java.io.IOException;
import java.io.InputStream;
public class Receive {
private InputStream in = null;
public Receive(InputStream in) {
this.in = in;
}
public String parse() {
StringBuffer receiveStr = new StringBuffer(2048);
int i;
byte[] bytes = new byte[2048];
try {
i = in.read(bytes);
} catch (IOException e) {
i = -1;
e.printStackTrace();
}
for (int j = 0; j < i; j++) {
receiveStr.append((char)bytes[j]);
}
return getUri(receiveStr.toString());
}
private String getUri(String receiveStr) {
System.out.println("===============receiveStr===================");
System.out.println(receiveStr);
System.out.println("===============receiveStr===================");
int index1, index2;
index1 = receiveStr.indexOf(' ');
if (index1 != -1) {
index2 = receiveStr.indexOf(' ', index1 + 1);
if (index2 > index1) {
return receiveStr.substring(index1 + 1, index2);
}
}
return null;
}
}
- 设置响应转发,处理地址请求
package com.study.diyserver;
import java.io.*;
public class Answer {
private OutputStream outs;
public Answer(OutputStream outs) {
this.outs = outs;
}
public void Send(String pagefile) throws IOException {
byte[] bytes = new byte[2048];
FileInputStream fis = null;
File file = new File(HttpServer.ROOT, pagefile);
System.out.println(file.getPath());
if (file.exists()) {
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, 2048);
String sBody = new String(bytes, 0);
String sendMessage = "HTTP/1.1 200 OK\r\n" +
"Content-Type:text/html\r\b" +
"Content-Length:" + ch + "\r\n" +
"\r\n" + sBody;
outs.write(sendMessage.getBytes());
} else {
String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
"Content-Type:text/html\r\b" +
"Content-Length: 23\r\n" +
"\r\n" +
"<h1>File Not Found</h1>";
outs.write(errorMessage.getBytes());
}
if (fis != null) {
fis.close();
}
}
}
- 访问的页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>SUCCESS</h1>
</body>
</html>
参考《java程序员上班那点事儿》,强烈推荐!!!
|