当前位置:   article > 正文

简易web服务器_简易 web 服务器 开发一个简易的 web 服务器,它仅能处理一个请求。这个请求可以是

简易 web 服务器 开发一个简易的 web 服务器,它仅能处理一个请求。这个请求可以是

1、代码如下

package com.httpserver.server;

import java.io.*;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ServerThread implements Runnable {

    private static Map<String,String> contentMap = new HashMap<>();

    static {
        contentMap.put("html","text/html;charset=utf-8");
        contentMap.put("jpg","image/jpeg");
    }

    private Socket client;
    private InputStream ins;
    private OutputStream out;

    private PrintWriter pw;
    private BufferedReader br;

    public ServerThread(Socket client){
        this.client = client;
        init();
    }
    private void init(){
        try {
            ins = client.getInputStream();
            out = client.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            go();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static final String webroot = "e:\\webroot\\";


    private void go() throws IOException {
        //读取去请求内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
        String line = reader.readLine().split(" ")[1].replace("/","\\"); //请求的资源
        System.out.println(line);
        if(line.equals("\\")){
            line += "index.html";
        }

        //给用户响应
        PrintWriter pw = new PrintWriter(out);

        //文件在e:\webroot\index.html下
        InputStream i = new FileInputStream(webroot + line);
//        BufferedReader fr = new BufferedReader(new InputStreamReader(i));

        pw.println("HTTP/1.1 200 OK");
        String s = line.substring(line.lastIndexOf(".")+1,line.length());
        System.out.println(s);
        pw.println("Content-Type: "+contentMap.get(s));
        pw.println("Content-Length: " +i.available());
        pw.println("Server: Hello" );
        pw.println("Data:" + new Date());
        pw.println();
        pw.flush();
        byte[] buff = new byte[1024];
        int leg = 0;
        while ((leg = i.read(buff)) != -1){
            out.write(buff,0,leg);
        }
        pw.flush();

        i.close();
        pw.close();
        reader.close();

        client.close();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

2、测试代码

package com.httpserver.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpServer3 {

    public static void main(String[] args) throws IOException {

        ExecutorService pool = Executors.newCachedThreadPool();

        //启动服务器,监听8888端口
                ServerSocket server = new ServerSocket(8888);
        System.out.println("服务器启动,监听" + 8888 + "端口。");

        while (!Thread.interrupted()){
            //不接收接收客户端请求
           Socket client = server.accept();
            pool.execute(new ServerThread(client));
        }
        server.close();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3、文件目录结构
在这里插入图片描述
(1)index.html的文件内容如下:
在这里插入图片描述
(2)login.html的文件内容如下:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/216976
推荐阅读
相关标签
  

闽ICP备14008679号