Java简单代码:java.net.SocketException:来自服务器的文件意外结束 [英] Java simple code: java.net.SocketException: Unexpected end of file from server

查看:457
本文介绍了Java简单代码:java.net.SocketException:来自服务器的文件意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中编写了一些简单的代码,该方法应该连接到网站并返回BufferedReader。

I wrote some simple code in Java, the method should connect to the website and return the BufferedReader.

private BufferedReader getConnection(String url_a) {
        URL url;
        try {
            System.out.println("getting connection");
            url = new URL(url_a);
            HttpURLConnection urlConnection = (HttpURLConnection) 
                     url.openConnection();
            urlConnection.addRequestProperty("User-Agent",
                    "Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.3) Gecko/20040924"
                     + "Epiphany/1.4.4 (Ubuntu)");
            inStream = new InputStreamReader(urlConnection.getInputStream());
            return new BufferedReader(inStream);
        } catch (Exception ex) {
            Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

}

当我在我的电脑上使用它时,它工作正常但是当我在服务器上放置.jar文件时,我收到此错误:

When I use it on my PC, it works fine but when I put .jar file on the server I get this error:

java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:718)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:715)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322)
at dataconverter.Reader.getConnection(Reader.java.260)

问题很奇怪,因为每次都不抛出异常,有时候一切正常,程序运行正常。

Problem is quite strange because the exception isn't thrown each time, sometimes everything is OK and program works fine.

有没有人有任何想法?

推荐答案

意外的文件结束意味着远程服务器接受并关闭了连接而不发送响应。远程系统可能太忙而无法处理请求,或者存在随机丢弃连接的网络错误。

"Unexpected end of file" implies that the remote server accepted and closed the connection without sending a response. It's possible that the remote system is too busy to handle the request, or that there's a network bug that randomly drops connections.

根据可用信息,无法说明发生了什么错误。如果您可以访问相关服务器,则可以使用数据包嗅探工具查找确切的发送和接收内容,并查看服务器进程的日志以查看是否有任何错误消息。

With the information available it's impossible to say what's going wrong. If you have access to the servers in question you can use packet sniffing tools to find what exactly is sent and received, and look at logs to of the server process to see if there are any error messages.

这篇关于Java简单代码:java.net.SocketException:来自服务器的文件意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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