我写了一个简单的代码,它是一个细小的Web服务器。我想在客户端输入loalhost:8181/pic时显示图片,但是当我String str = in.readLine() str仅显示localhost:8181时,我如何才能在其浏览器中找到客户端,请输入localhost:8181/pic?有我的简单代码:

    protected void start() {
    ServerSocket s;

    System.out.println("Webserver starting up on port 80");
    System.out.println("(press ctrl-c to exit)");
    try {
        // create the main server socket
        s = new ServerSocket(8181);
    } catch (Exception e) {
        System.out.println("Error: " + e);
        return;
    }

    System.out.println("Waiting for connection");
    for (;;) {
        try {
            Socket remote = s.accept();
            System.out.println("Connection, sending data.");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    remote.getInputStream()));
            PrintWriter out = new PrintWriter(remote.getOutputStream());
            String method = in.readLine();
            out.flush();
            remote.close();
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }
}

最佳答案

您应该采用所有参数。不只是两个第一线;

    String line;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}

关于java - 获取网络类的URL地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25316550/

10-10 13:37