溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java webserver-封裝響應(yīng)協(xié)議

發(fā)布時間:2020-06-26 02:37:32 來源:網(wǎng)絡(luò) 閱讀:241 作者:wx5d21d5e6e5ab1 欄目:編程語言

Response:

public class Response {
    private BufferedWriter bw;
    private Socket client;
    private StringBuilder headInfo; //協(xié)議頭包括狀態(tài)行和請求頭和回車
    private StringBuilder content;
    private int len=0; //正文的字節(jié)數(shù)
    private final String BLANK=" ";
    private final String CRLF="\r\n";

    private Response()
    {
        content=new StringBuilder();
        headInfo=new StringBuilder();
        len=0;
    }
    public Response(Socket client)
    {
        this(); //調(diào)用默認(rèn)構(gòu)造器
        try {
            bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        } catch (IOException e) {

            e.printStackTrace();
            headInfo=null;
        }
    }

    public Response(OutputStream os)
    {
        this();  //調(diào)用默認(rèn)構(gòu)造器
        bw=new BufferedWriter(new OutputStreamWriter(os));
    }
    public Response print(String info)   //流模式,不斷添加內(nèi)容返回自身
    {
        content.append(info);
        len+=info.getBytes().length;
        return this;
    }
    public Response println(String info)
    {
        content.append(info).append(CRLF);
        len+=(info+CRLF).getBytes().length;  //換行符也占長度
        return this;

    }
    //推送響應(yīng)信息
    public void pushToBrowser(int code) throws IOException
    {
        if(null==headInfo) {
            code=505;
        }
        createHeadInfo(code);

            bw.append(headInfo);  //bw.append()以追加模式寫出數(shù)據(jù),.write()刷新原有數(shù)據(jù),只有當(dāng)前數(shù)據(jù)
            bw.append(content);
            bw.flush();

    }
    //構(gòu)建頭信息
    private void createHeadInfo(int code)  //傳不同的code有不同的狀態(tài)
    {
        //1、響應(yīng)行:HTTP/1.1 200 OK
        headInfo.append("HTTP/1.1").append(BLANK);
        headInfo.append(code).append(BLANK);
        switch(code)
        {
            case 200:
                headInfo.append("OK").append(CRLF);
                break;
            case 404:
                headInfo.append("NOT FOUND").append(CRLF);
                break;
            case 505:
                headInfo.append("SERVER ERROR").append(CRLF);
                break;
        }
        //2、響應(yīng)頭
        headInfo.append("Date:").append(new Date()).append(CRLF);
        headInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
        headInfo.append("Content-type:text/html").append(CRLF);
        headInfo.append("Content-length:").append(len).append(CRLF);
        headInfo.append(CRLF); //響應(yīng)頭和正文之間有空行

    }
}

Server:

public class Server02 {
    private ServerSocket serverSocket ;
    public static void main(String[] args) {
        Server02 server = new Server02();
        server.start();
    }
    //啟動服務(wù)
    public void start() {
        try {
            serverSocket =  new ServerSocket(8888);
             receive();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服務(wù)器啟動失敗....");
        }
    }
    //接受連接處理
    public void receive() {
        try {
            Socket client = serverSocket.accept();
            System.out.println("一個客戶端建立了連接....");
            //獲取請求協(xié)議
            InputStream is =client.getInputStream();
            byte[] datas = new byte[1024*1024];
            int len = is.read(datas);
            String requestInfo = new String(datas,0,len);
            System.out.println(requestInfo);

            //關(guān)注了內(nèi)容
            Response response=new Response(client); //創(chuàng)建好了輸出流
            response.print("<html>");    //通過輸出流輸出
            response.print("<head>");
            response.print("<title>");
            response.print("服務(wù)器響應(yīng)成功");
            response.print("</title>");
            response.print("</head>");
            response.print("<body>");
            response.print("shsxt server終于回來了。。。。");
            response.print("</body>");
            response.print("</html>");
            //關(guān)注了狀態(tài)碼
            response.pushToBrowser(200);

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("客戶端錯誤");
        }
    }
    //停止服務(wù)
    public void stop() {

    }

}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI