Http连接读取超时

时间:2014-05-02 07:06:03

标签: java swing java-ee servlets urlconnection

亲爱的朋友们,我希望代码能够设置给定网址的响应时间,特别是给定秒数,否则应该是错误

这里我附上了拥有730MB Iso文件下载页面的代码,最低需要30分钟。在这里,我只需要10秒即可下载文件并从该页面获得响应,我确信30分钟下载文件它不会在10秒内给出响应,但这里给出了适当的响应。

请在这里检查我的代码并告诉我我错误的地方..

public class test {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        // TODO Auto-generated method stub
        nodeBIT("");
    }
    private static void nodeBIT(String string) throws ClientProtocolException, IOException {
        String url1 = "http://download.microsoft.com/download/e/e/9/ee9d0116-c9fe-4fc2-b59c-406cbfb6d515/xpsp3_5512.080413-2113_usa_x86fre_spcd.iso";
        URL obj = new URL(url1); URLConnection con = obj.openConnection(); 
        con.setConnectTimeout(10000); 
        con.setReadTimeout(10000); // 10 seconds to read the file
        InputStreamReader input = new InputStreamReader(con.getInputStream()); 
        BufferedReader in = new BufferedReader(input);
        String inputLine;
        String fullline = "";
        while ((inputLine = in.readLine()) != null)
        { 
            fullline = fullline.concat(inputLine); 
        } 
        System.out.println(fullline);

    }
}

3 个答案:

答案 0 :(得分:2)

API

  

“如果超时在有可用于读取的数据之前到期”

这意味着一旦数据可供下载,连接就不会超时。如果你的服务器甚至开始将数据发送回客户端需要10秒以上的时间,那就是超时启动时。

答案 1 :(得分:2)

只要收到数据,就不会按javadocs

超时

您可以修改循环

    long se = System.currentTimeMillis();
    while ((inputLine = in.readLine()) != null)
    { 
        fullline = fullline.concat(inputLine); 
        if ( System.currentTimeMillis() > se + 10000) {
             throw new IOException ();
        }
    } 

答案 2 :(得分:-1)

检查服务器上的超时。因为当你试图通过JAVA下载大文件时,服务器会给出超时异常。

这里有一些其他的示例代码,用于使用FTP将数据从服务器下载到应用程序。我使用了两个列表remoteFileList,localFileList来提供sourse文件和本地文件路径。

            FTPClient ftpClient = new FTPClient();
        ftpClient.connect(Constant.FTP_SERVER_IP);
        ftpClient.login(Constant.FTP_USER_NAME, Constant.FTP_PASSWORD);

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();

        int reply = ftpClient.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Connection Succeeded");
            for (int i = 0; i < remoteFileList.size(); i++) {
                remoteFileName = remoteFileList.get(i);
                localFileName = localFileList.get(i);
                File localFile = new File(localFileName);
                if (localFile.exists()) {
                    System.out.println("File Already Exists!!");
                } else {
                    downloadFile = new FileOutputStream(localFile);
                    boolean isDownloaded = ftpClient.retrieveFile(remoteFileName, downloadFile);
                    System.out.println("Downloaded");
                    if (isDownloaded) {
                       System.out.println(" - Success.\n");
                    } else {
                       System.out.println( " - Unsuccess.\n");
                    }

                }
                if (i == remoteFileList.size() - 1 && this.DownloadType == 1) {
                    System.out.println("This is time to open !");
                    JOptionPane.showMessageDialog(this, "Image downloading completed.Please open the inspection");
                }
            }
        } else {
            ftpClient.disconnect();
        }
相关问题