Java字符串为空,但打印出来之前不为空

Java字符串为空,但打印出来之前不为空,java,string,Java,String,我想阅读一些HTTP Requestst,并有以下内容: public class HTTPServerThread extends Thread { private Socket socket = null; private BufferedOutputStream out; public HTTPServerThread(Socket socket) { super("HTTPServerThread"); this.socket = socket; } public v

我想阅读一些HTTP Requestst,并有以下内容:

public class HTTPServerThread extends Thread {
private Socket socket = null;
private BufferedOutputStream out;

public HTTPServerThread(Socket socket) {
    super("HTTPServerThread");
    this.socket = socket;
}

public void run() {
    try {
        out = new BufferedOutputStream(socket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(new DataInputStream(socket.getInputStream())));

        String request = "";
        String temp;
        while ((temp = in.readLine()) != null) {
            request += temp+"\n";
            System.out.println("request:\n"+request);
        }
        System.out.println("request final:\n"+request);
        if (request.equals("")) System.out.println("Request empty");

        out.close();
        socket.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

当我运行代码并在服务器上请求某些内容时,请求会在while循环中一部分一部分地打印出来,所以一切都正常。但当我最终打印出来时,它会给我“”并打印出“请求为空”,我不知道我(可能是愚蠢的)错误是什么,所以请帮助我:)

电流输出:

request:
GET / HTTP/1.1

request:
GET / HTTP/1.1
Host: localhost

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de,en-US;q=0.8,en;q=0.6

request:
GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.0 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de,en-US;q=0.8,en;q=0.6


request final:

Request empty
类与主类:

public class HTTPServer {

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    int port;
    boolean listening = true;

    //read port from args[0] if exists, else port = 80

    if (args.length == 0) {
        port = 80;
    }
    else {
        port = Integer.parseInt(args[0]);
    }

    //start listening on port

    try {
        serverSocket = new ServerSocket(port);
    }
    catch (IOException e) {
        System.out.println("Could not listen on port: "+port);
        System.exit(-1);
    }

    System.out.println("Java HTTP-Server");
    System.out.println("Port: " + port);

    //start new Thread if someone wants to connect

    while (listening)
        new HTTPServerThread(serverSocket.accept()).start();

    serverSocket.close();
}

您应该将ip和端口连接到您的套接字

你的插座不知道去哪里

使用以下命令:

Socket Socket=新的套接字(int ip,int端口)

正如所猜测的,问题是由于原始线程在循环时卡在
中,而第二个线程实际打印空字符串

while
循环的最后一次迭代会产生一个空的但不是空的
temp
字符串,这可以从看似重复的请求输出中看出,在线程卡住之前有一个额外的空行

因此,将停止条件更改为

while ((temp = in.readLine()) != null && temp.length() > 0) {

将允许原始线程退出循环并打印正确的最终请求。

使用
StringBuilder
来concat仅供参考!你能发布当前的输出吗?用你的代码,用一个文件来获取数据,它工作得很好。这是从多个线程调用的吗?@JonathanDrapeau:request似乎是一个局部变量,而不是线程之间共享的字段。您可能有两个线程并行运行。第一个打印它接收到的第7行,然后在其循环中被阻止,因为服务器不再发送任何内容,第二个打印它从服务器接收一个空响应,并打印它的空响应。将当前线程添加到日志语句中以确认或削弱。-1 post和+1@Arthur我认为OP已经从其套接字接收到数据,因此我们可以假设它设置正确。这与问题有什么关系?如果你愿意的话,可以添加评论之类的东西。我没有收到任何东西的问题,它也只需要将第一行直接读入请求并打印出来就可以了。。。