用来实现b/s之间通信,把服务器中的内容共享给外部。
1 public class Server { 2 /** 3 * socket服务器端程序 4 * @param args 5 */ 6 public static void main(String[] args) throws Exception{ 7 //1.创建ServerSocket 8 ServerSocket server = new ServerSocket(8888);//8888为端口 9 System.out.println("服务器已经启动成功·········"); 10 11 //2.接收客户端的连接 12 Socket socket = server.accept(); 13 14 //3.读取本地的test.html文件 15 FileInputStream in = new FileInputStream(new File("C:\\Users\\MaoDoer\\Desktop\\test.html")); 16 17 //4.构造数据输出通道 18 OutputStream out = socket.getOutputStream(); 19 20 //5.发送数据 21 byte[] buf=new byte[1024]; 22 int len=0; 23 while( (len=in.read(buf))!=-1 ){ 24 out.write(buf, 0, len); 25 } 26 27 //6.关闭资源 28 out.close(); 29 in.close(); 30 } 31 }