版本1
但是只能实现一次访问,不可以接受刷新
package front_end.day01_html.myServer;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class MyBrowseServer {
public static void main(String[] args) throws IOException {
System.out.println("服务器正在连接.....");
while (true) {
method();
}
}
public static void method() throws IOException {
ServerSocket serverSocket = new ServerSocket(8000);
Socket accept = serverSocket.accept();
InputStream is = accept.getInputStream();
InputStreamReader reader = new InputStreamReader(is);
BufferedReader br = new BufferedReader(reader);
String request = br.readLine();
String[] str = request.split(" ");
String path = str[1].substring(1);
FileInputStream input = new FileInputStream(path);
OutputStream out = accept.getOutputStream();
out.write("HTTP/1.1 200 OK\r\n".getBytes());
out.write("Content‐Type:text/html\r\n".getBytes());
out.write("\r\n".getBytes());
byte[] bytes = new byte[1024];
int len;
while ((len = input.read(bytes)) != -1){
out.write(bytes, 0, len);
}
out.close();
is.close();
br.close();
accept.close();
serverSocket.close();
}
}
|