简易apache Server 服务器

自我学习的时候,参考学习视频手写的一个apache Server 服务器,用于理解Apache 服务器原理。
WebServer.java 文件 启动 监听80端口

package com.learn.demo;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class WebServer {
    public void serverStart(int port) {
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            while (true) {
                Socket socket = serverSocket.accept();
                new Processor(socket).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        int port=80;
        if(args.length==1){
            port=Integer.parseInt(args[0]);
        }
        new WebServer().serverStart(port);
    }

}


Processor.java  处理接收的html 文件,并通过输出流http文本内容

package com.learn.demo;

import java.io.*;
import java.net.Socket;


public class Processor extends Thread {
    private Socket socket;
    private InputStream in;
    private PrintStream out;

    private final  static String WEB_ROOT="E:\\myProjects\\webServer\\httpdocs";
    public Processor(Socket socket) {
        this.socket = socket;
        try {
            in = socket.getInputStream();
            out=new PrintStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
          String filename=parse(in);
          sendFile(filename);
    }

    public String parse(InputStream in) {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String fileName = null;
        try {
            String httpMessage = br.readLine();
            String[] content = httpMessage.split(" ");
            if (content.length != 3) {
                sendErrorMessage(400, "Client query error");
                return null;
            }
            System.out.println("code:" + content[0] + ", filename" + content[1] + ", http version" + content[2]);
            fileName = content[1];
        } catch (IOException e) {
            e.printStackTrace();
        }

        return fileName;
    }

    private void sendErrorMessage(int errorCode, String errorMessage) {
        out.println("HTTP/1.0 "+errorCode+" "+errorMessage);
        out.println("content-type: text/html");
        out.println();
        out.println("<html>");
        out.println("<title>Error Message");
        out.println("</title>");
        out.println("<body>");
        out.println("<h1>ErrorCode:"+errorCode+",ErrorMeesage:"+errorMessage);
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendFile(String fileName) {
       File file=new File(Processor.WEB_ROOT+fileName);
        if(!file.exists()||fileName.equals("/")){
            sendErrorMessage(404,"File Not Found");
            return;
        }
        try {
            InputStream in=new FileInputStream(file);
            byte [] content=new byte[(int)file.length()];
            in.read(content);
            out.println("HTTP/1.0 200 queryfile");
            out.println("content-length:"+content.length);
            out.println();
            out.write(content);

            out.flush();
            out.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值