为什么代码显示“错误354(net :: ERR_CONTENT_LENGTH_MISMATCH):服务器意外关闭了连接。” [英] Why code shows "Error 354 (net::ERR_CONTENT_LENGTH_MISMATCH): The server unexpectedly closed the connection."

查看:265
本文介绍了为什么代码显示“错误354(net :: ERR_CONTENT_LENGTH_MISMATCH):服务器意外关闭了连接。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 JAVA 中构建 HTTP WEB SERVER

如果客户端请求任何文件和该文件在服务器上的那个地方,然后服务器将该文件提供给客户端。我也制作了这个代码,它运行正常。

If client request any file and that file is on that place in server, then server gives that file to client. I also made this code, and it works fine.

代码中显示上述功能的部分

File targ = [CONTAINS ONE FILE]
PrintStream ps;
InputStream is = new FileInputStream(targ.getAbsolutePath());
            while ((n = is.read(buf)) > 0) {
                System.out.println(n);
                ps.write(buf, 0, n);
            }  

但现在为了优化我的代码,我用以下代码替换下面的代码,

   InputStream is = null;
    BufferedReader reader = null;
    String output = null;

    is = new FileInputStream(targ.getAbsolutePath());
    reader = new BufferedReader(new InputStreamReader(is));

        while( (output = reader.readLine()) != null) {
           System.out.println("new line");
           //System.out.println(output);
           ps.print(output);
        }

但它有时会显示一个错误为什么代码显示错误354(net :: ERR_CONTENT_LENGTH_MISMATCH):服务器意外关闭了连接。。我不明白,为什么它显示这个错误。这个错误非常奇怪,因为服务器显示 200 代码,这意味着,该文件就在那里。

But it sometimes shows one error Why code shows "Error 354 (net::ERR_CONTENT_LENGTH_MISMATCH): The server unexpectedly closed the connection.". I didn't understand, why it shows this error. This error is very weird, because server shows 200 code, that means, that file is there.

帮帮我拜托。

编辑号。 1

    char[] buffer = new char[1024*16];
    int k = reader.read(buffer);
    System.out.println("size : " + k);
    do { 
       System.out.println("\tsize is : " + k);
       //System.out.println(output);
       ps.println(buffer);
    }while(  (k = reader.read(buffer)) != -1 );

这会打印所有文件,但对于较大的文件,它会显示不可读的字符。

This prints all the file, but for bigger files, it shows unreadable characters.

显示以下输出 (客户端浏览器的快照)

< img src =https://i.stack.imgur.com/B1FPx.jpgalt =在此处输入图像说明>

推荐答案

您执行 output = reader.readLine()以获取数据,该数据省略了换行符。然后你 ps.print(输出),所以新行字符不会发送到客户端。

You do output = reader.readLine() to get the data, which omits the newline characters. Then you ps.print(output), so the newline characters are not sent to the client.

说你阅读本文

Hello\r\n
World\r\n

然后你发送:

Content-length: 14

HelloWorld

然后关闭连接,混淆了浏览器,因为它仍在等待其他4个字节。

And then close the connection, confusing the browser as it still was waiting for the other 4 bytes.

我想你必须使用 ps.println(输出)

如果您正在监控网络流量,您会看到这一点,这在编写或调试应该使用网络进行通信的服务器时非常有用。

You would have seen this if you were monitoring the network traffic, which can prove quite useful when writing or debugging a server that is supposed to communicate using the network.

无论如何,如果文件的换行符和系统不匹配,这将导致麻烦( \ n vs \r\\\
)。假设您有这个文件:

Anyway this will cause trouble if the newlines of the file and the system have a mismatch (\n vs \r\n). Say you have this file:

Hello\r\n
World\r\n

其长度为14个字节。但是,当您的系统在打印为 \ n 时处理换行符时,带有 println()的代码将打印出:

Its length is 14 bytes. However when your system treats a newline when printing as \n, your code with println() will print this:

Hello\n
World\n

这是12个字节,而不是14个。你最好只打印你所读的内容。

Which is 12 bytes, not 14. You better just print what you read.

这篇关于为什么代码显示“错误354(net :: ERR_CONTENT_LENGTH_MISMATCH):服务器意外关闭了连接。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆