Java 实现简单静态资源Web服务器的示例

需求

有时候我们想快速通过http访问本地的一些资源,但是安装一些web服务器又很费时和浪费资源,而且也不是长期使用的。

这时候我们可以启动一个小型的java服务器,快速实现一个http的静态资源web服务器。

难点

其实没什么难点,主要是注意请求头和返回头的处理。然后将请求的文件以流的方式读入返回outputstream即可。

代码

直接上代码吧~

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.InetAddress;

import java.net.ServerSocket;

import java.net.Socket;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

 

public class ResourceWebServer {

    private static final int SERVER_PORT = 8888;

    private static final int MAX_CONNECTION_LENGTH = 1;

 

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

        log("======服务器启动=====");

        ResourceWebServer server = new ResourceWebServer();

        server.startServer();

    }

 

    public void startServer() throws IOException {

        ServerSocket serverSocket = new ServerSocket(SERVER_PORT, MAX_CONNECTION_LENGTH, InetAddress.getByName("localhost"));

 

        log("======准备接收请求=====");

        while (true) {

            Socket socket = serverSocket.accept();

            try (InputStream inputStream = socket.getInputStream();

                 OutputStream outputStream = socket.getOutputStream()) {

 

                String requestUri = getRequestUri(inputStream);

                log("请求文件:" + requestUri);

 

                writeHeaders(outputStream);

 

                Path path = Paths.get(getClass().getClassLoader().getResource(requestUri.substring(1)).toURI());

                Files.copy(path, outputStream);

            } catch (Exception e) {

                log("发生错误啦!");

                e.printStackTrace();

            }

        }

    }

 

    private void writeHeaders(OutputStream outputStream) throws IOException {

        //必须包含返回头,否则浏览器不识别

        outputStream.write("HTTP/1.1 200 OK\r\n".getBytes());

        //一个\r\n代表换行添加新的头,2次\r\n代表头结束

        outputStream.write("Content-Type: text/html\r\n\r\n".getBytes());

    }

 

    private String getRequestUri(InputStream inputStream) throws IOException {

        StringBuilder stringBuilder = new StringBuilder(2048);

        byte[] buffer = new byte[2048];

        int size = inputStream.read(buffer);

 

        for (int i = 0; i < size; i++) {

            stringBuilder.append((char) buffer[i]);

        }

 

        String requestUri = stringBuilder.toString();

        //此时的uri还包含了请求头等信息,需要去掉

        //GET /index.html HTTP/1.1...

        int index1, index2;

        index1 = requestUri.indexOf(" ");

        if (index1 != -1) {

            index2 = requestUri.indexOf(" ", index1 + 1);

            if (index2 > index1) {

                return requestUri.substring(index1 + 1, index2);

            }

        }

        return "";

    }

 

    private static void log(Object object) {

        System.out.println(object);

    }

}

接下来,就可以在resource文件下放入静态资源啦,比如放一个index.html

然后启动,打开浏览器输入http://localhost:8888/index.html就能看到结果了!

以上是 Java 实现简单静态资源Web服务器的示例 的全部内容, 来源链接: utcz.com/z/327800.html

回到顶部