标签:input stat inpu 输入 except 指定 byte pat get
1 import java.io.BufferedInputStream; 2 import java.io.BufferedOutputStream; 3 import java.io.BufferedReader; 4 import java.io.FileInputStream; 5 import java.io.InputStreamReader; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 public class MyTomcat { 10 public static void main(String[] args) throws Exception { 11 // 创建服务端的ServerSocket对象并监听指定的端口号 12 ServerSocket ss = new ServerSocket(8080); 13 // 循环的接收客户端的连接 14 while (true) { 15 Socket socket = ss.accept(); 16 // 将字节输入流包装成字符缓冲输入流 17 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 18 String line = br.readLine();// GET /register.html HTTP/1.1 19 if (null != line) {// 如果line不为null,继续后面的步骤 20 // 把对应的页面返回给浏览器客户端 21 String path = "webapps" + line.split(" ")[1];// webapps/register.html 22 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path)); 23 BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); 24 25 // 输出成功的信号给浏览器,必须要有 26 bos.write("HTTP/1.1 200 ok \r\n\r\n".getBytes()); 27 28 // 把对应的register.html输出给浏览器 29 byte[] buf = new byte[1024]; 30 int len; 31 while ((len = bis.read(buf)) != -1) { 32 bos.write(buf, 0, len); 33 } 34 //bos.write("登录成功".getBytes("gbk")); 35 //bos.write("账号或密码错误".getBytes("gbk")); 36 bos.flush(); 37 bis.close(); 38 } 39 // 关闭资源 40 socket.close(); 41 } 42 } 43 }
标签:input stat inpu 输入 except 指定 byte pat get
原文地址:https://www.cnblogs.com/wangjiaxi/p/9534672.html