简单web服务器

一个很简单的web服务器。
import java.io.*;
import java.net.*;
public class MyServer{
    public static void main(String[] args){

        try{
       //建立连接,开启端口 
        ServerSocket serverSocket = new ServerSocket(8888); 
        //获取连接
        Socket socket = serverSocket.accept();
        //获取输出流对象
        OutputStream out = socket.getOutputStream();
        //获取输入流对象
        InputStream in = new FileInputStream("D:\\hello.html"); 
        int i = -1;
        byte[] b = new byte[1024];
        while((i = in.read(b)) != -1){
            //直接在页面
            out.write(b,0,i);
        }
        in.close();
        socket.close();
        }catch(Exception e){
            System.out.println(e);
        }
    }
}
从浏览器访问 http://localhost:8888,即可访问到hello.html页面。
原文地址:https://www.cnblogs.com/yangzhi/p/3576647.html