亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java 實現(xiàn)簡單靜態(tài)資源Web服務(wù)器的示例

 更新時間:2020年11月12日 14:56:33   作者:派大星  
這篇文章主要介紹了Java 實現(xiàn)簡單靜態(tài)資源Web服務(wù)器的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下

需求

有時候我們想快速通過http訪問本地的一些資源,但是安裝一些web服務(wù)器又很費(fèi)時和浪費(fèi)資源,而且也不是長期使用的。

這時候我們可以啟動一個小型的java服務(wù)器,快速實現(xiàn)一個http的靜態(tài)資源web服務(wù)器。

難點

其實沒什么難點,主要是注意請求頭和返回頭的處理。然后將請求的文件以流的方式讀入返回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("======服務(wù)器啟動=====");
        ResourceWebServer server = new ResourceWebServer();
        server.startServer();
    }
 
    public void startServer() throws IOException {
        ServerSocket serverSocket = new ServerSocket(SERVER_PORT, MAX_CONNECTION_LENGTH, InetAddress.getByName("localhost"));
 
        log("======準(zhǔn)備接收請求=====");
        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("發(fā)生錯誤啦!");
                e.printStackTrace();
            }
        }
    }
 
    private void writeHeaders(OutputStream outputStream) throws IOException {
        //必須包含返回頭,否則瀏覽器不識別
        outputStream.write("HTTP/1.1 200 OK\r\n".getBytes());
        //一個\r\n代表換行添加新的頭,2次\r\n代表頭結(jié)束
        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文件下放入靜態(tài)資源啦,比如放一個index.html

然后啟動,打開瀏覽器輸入http://localhost:8888/index.html就能看到結(jié)果了!

以上就是Java 實現(xiàn)簡單靜態(tài)資源Web服務(wù)器的示例的詳細(xì)內(nèi)容,更多關(guān)于java 實現(xiàn)web服務(wù)器的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決Mac?m1?電腦?idea?卡頓的問題

    解決Mac?m1?電腦?idea?卡頓的問題

    這篇文章主要介紹了Mac?m1?電腦?idea?卡頓的問題解決,文中給大家補(bǔ)充介紹了IDEA卡頓問題處理方法,需要的朋友可以參考下
    2023-03-03
  • 使用jpa之動態(tài)插入與修改(重寫save)

    使用jpa之動態(tài)插入與修改(重寫save)

    這篇文章主要介紹了使用jpa之動態(tài)插入與修改(重寫save),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • maven依賴的version聲明控制方式

    maven依賴的version聲明控制方式

    這篇文章主要介紹了maven依賴的version聲明控制方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 最新評論