通过Socket搭建Web服务器 package com.bean;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class Main {/** * @param ar
package com.bean; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Main { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //1,创建Socket服务器 ServerSocket serverSocket=new ServerSocket(8080); System.out.println("服务器启动了!"); while(true){ //2,建立与客户端的链接 Socket accept=serverSocket.accept(); System.out.println("客户端来了!"); //3.获取本地的test.html FileInputStream fileInputStream=new FileInputStream(new File("G:/Java workspaces/test.html")); //4,建立沟通数据管道 OutputStream outputStream=accept.getOutputStream(); //5,读取完整发送到客户端 byte[] bty=new byte[1024]; int len=0; while((len=fileInputStream.read(bty))!=-1){ outputStream.write(bty,0,len); } fileInputStream.close(); outputStream.close(); } } }