美文网首页
支持静态页面的简易WebServer

支持静态页面的简易WebServer

作者: 红Archer | 来源:发表于2017-12-27 22:37 被阅读0次

    Server

    package cn.com.demo.tomcat;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.Socket;
    
    public class RequestDealThread extends Thread{
        private Socket socket;
        public RequestDealThread(Socket socket)
        {
            this.socket = socket;
        }
        
        public void run()
        {
            try{
                InputStream in = socket.getInputStream();
                OutputStream out = socket.getOutputStream();
                //1、获取请求内容
                InputStreamReader reader = new InputStreamReader(in);
                char[] chars = new char[1024];
                int count = reader.read(chars);
                //2、获取请求名称
                String reqStr = new String(chars,0,count);
                String[] lines = reqStr.split("\n");//获取到请求信息后,用split通过换行分割
                String reqName = null;
                if(lines != null&&lines.length>0)
                {
                    String firstLine = lines[0];//split分割后的第一行
                    String[] firstLines = firstLine.split(" ");//将分割后的第一行信息用split通过空格分割
                    if(firstLines != null && firstLines.length==3)
                    {
                        reqName = firstLines[1];//得到分割后的第二个字符串
                        System.out.println(reqName);
                    }
                }
                //3、根据资源请求名称,读取文件内容
                InputStream resourceIN = this.getClass().getResourceAsStream(reqName);
                //4、将内容和响应头信息合并,写入输出流
                out.write("HTTP/1.1 200 OK\n".getBytes());
                out.write("Server: Apache-Coyote/1.1\n".getBytes());
                out.write("Date: Wed, 27 Dec 2017 12:59:42 GMT\n".getBytes());
                out.write("\n".getBytes());
                byte[] bytes = new byte[1024];
                while((count = resourceIN.read(bytes))!=-1)
                {
                    out.write(bytes,0,count);
                }
                out.flush();
                in.close();
                resourceIN.close(); 
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally{
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
    
    
    }
    
    

    Test方法

    package cn.com.demo.tomcat;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketException;
    
    public class TomcatServer {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                ServerSocket server = new ServerSocket(8087);
                while(true)
                {
                Socket socket = server.accept();
                new RequestDealThread(socket).start();
                }
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (Exception e) {
                // TODO: handle exception
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:支持静态页面的简易WebServer

          本文链接:https://www.haomeiwen.com/subject/mtbpgxtx.html