这里仅仅需要编辑服务器的代码,因为浏览器就可以是客户端对象
实现步骤:
1.将已存在的静态页面对象放在 项目下
2.创建server套接字
3.实现服务器一直开启的状态
4.开启多线程技术,提高效率
ServerSocket server = new ServerSocket(8080);while (true){Socket sc = server.accept();new Thread(new Runnable() {@Overridepublic void run() {try {InputStream is = sc.getInputStream();if (is != null) {BufferedReader bw = new BufferedReader(new InputStreamReader(is));String line;line = bw.readLine();String[] path = line.split(" ");String filename = path[1].substring(1); System.out.println("filename:" + filename);FileInputStream fis = new FileInputStream(filename);OutputStream os = sc.getOutputStream();os.write("HTTP/1.1 200 OK\r\n".getBytes());os.write("Content-Type:text/html\r\n".getBytes());os.write("\r\n".getBytes());int len = 0;byte[] bytes = new byte[1024];while ((len = fis.read(bytes)) != -1) {os.write(bytes, 0, len);}fis.close();sc.close();}} catch (IOException e) {e.printStackTrace();}}}).start();}