浏览器输入网址localhost:8888可共享文件1.txt.
public class demo1 { public static void main(String[] args) throws Exception{ //1.创建ServerSocket套接字 ServerSocket server = new ServerSocket(8888);//监听8888端口 System.out.println("服务器已经开启..."); //2.接收客户端的连接 Socket socket = server.accept(); //3.读取本地html文件 FileInputStream inputStream = new FileInputStream(new File("E:\\workspace\\day8\\src\\demo1.html")); //4.构建数据输出通道 OutputStream outputStream = socket.getOutputStream(); //5.发送数据 byte[] buf = new byte[1024]; int len = 0; while((len=inputStream.read(buf))!=-1){ outputStream.write(buf,0,len); } //6.关闭资源 outputStream.close(); inputStream.close(); server.close(); } }