Question

Dear Friends here i want the code to set the time for response from given URL in particular given seconds else it should be error

Here i attached my code that having 730MB Iso file download page, its minimum takes 30min for me. Here I am giving just 10 seconds to download the file and getting the response from that page,I am sure 30min downloading file it wont give response in just 10 sec but here its giving proper response.

please check my code here and tel me where i did mistake..

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);

    }
}
Was it helpful?

Solution

as long as data is being received it will not timeout as per javadocs

You could modify your loop

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

OTHER TIPS

The API says that

"if the timeout expires before there is data available for read"

which means that once the data becomes available for download, then the connection will not timeout. If your server takes more than 10 seconds to even start sending the data back to the client, that's when timeout kicks in.

Check the time-out on the server too. Because when you trying to download large file through JAVA some time server is giving time out exception.

here are some other example code for downloading data from server to app using FTP.here i used two list remoteFileList,localFileList to give sourse file and local file paths.

            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();
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top