首页 > 解决方案 > Bufferedreader 停止读取

问题描述

阅读整个字符串后,阅读器停留在一段时间内,甚至不抛出异常。我正在通过 curl 向服务器发送请求。

我尝试更改 curl 的 Content-Type,更改字符串的内容,并使用另一种方式读取输入,例如扫描仪,但总是卡在 while。

curl -H "Content-Type: text/plain" -d "asdasdasdashdiasdasgdasgduascuyasvccccc" 192.168.0.59:8080
    // Read characters from the client via input stream on the socket
    in = new BufferedReader(new 
    InputStreamReader(connect.getInputStream()));
    // Get character output stream to client (for headers)
    out = new PrintWriter(connect.getOutputStream());
    // Get binary output stream to client (for requested data)
    dataOut = new BufferedOutputStream(connect.getOutputStream());

    // Get first line of the request from the client
    input = in.readLine();
    // Parse the request with a string tokenizer
    if (input == null || input.isEmpty()) {
        System.err.println("Invalid input");
        return;
    }

    parse = new StringTokenizer(input);

    method = parse.nextToken().toUpperCase();
    input = in.readLine();

    do {

        parse = new StringTokenizer(input);
        if (parse.hasMoreTokens()) {
            header = parse.nextToken().toUpperCase(); // we get the 
        HTTP method of the client
        }
        if (parse.hasMoreTokens()) {
            hValue = parse.nextToken().toLowerCase();
        }

        // Support only GET and HEAD methods, we check
        out.println(header + hValue);
        System.out.println(header + hValue);
        if (header.equals("CONTENT-TYPE:")) {
            readFile(in);
        }
        input = in.readLine();
    } while (!input.isEmpty());

    private void readFile(BufferedReader file) throws IOException {
        try {
            int count;
            count = 0;

            while ((count = file.read()) != -1) {
                System.out.print((char) count);
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            file.close();
        }
    }

日志:

Server started.

Listening for connections on port : 8080 ...

Connection opened. (Mon Aug 26 15:44:49 BRT 2019)

HOST:192.168.0.59:8080

USER-AGENT:curl/7.58.0

ACCEPT:*/*

CONTENT-TYPE:text/plain

Content-Length: 39

asdasdasdashdiasdasgdasgduascuyasvccccc

标签: javacurlserverinputstreambufferedreader

解决方案


似乎您的客户端(curl)没有关闭连接,因此

(count = file.read()) != -1

在读取最后一个字符后,计数被反复设置为 0,因此一直返回 true。

您需要确保客户端关闭连接,或者将消息长度与消息一起发送,并在收到发送的字符数时关闭它的服务器端(在 java 中)。

后者可以这样做:

1) 发送

curl -H "Content-Type: text/plain" -d "5asdas" 192.168.0.59:8080

2)更新代码以首先读取长度,然后倒计时直到0并像这样关闭连接

private void readFile(BufferedReader file) throws IOException {
    int expectedCount = -1;
    try {
            int count;
            count = 0;

            while ((count = file.read()) != -1 && (expectedCount > 0 || expectedCount == -1 ) ) {
                if ( expectedCount == -1 ) {
                    // read count and set in expectedCount
                } else {
                    //do what you need to do with the input
                    System.out.print((char) count);
                }
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            file.close();
        }
    }

请注意,此解决方案不考虑多位数长度之类的大量内容,但应该足以让您继续前进。


推荐阅读