Home > Back-end >  FileUtils.copyURLToFile Getting Stuck
FileUtils.copyURLToFile Getting Stuck

Time:11-09

I'm using a Jave program to get NSE share price data from NSE's website like this for example:

url = new URL("https://archives.nseindia.com/archives/equities/bhavcopy/pr/PR071122.zip");
f = new File("NSEData.zip");
try {
     FileUtils.copyURLToFile(url, f);
} catch (Exception e) {
}

The above code works for dates where market data exists, like 07/11/22 . However, where data does not exist, like on 08/11/22, the url is broken and the copyURLToFile line gets stuck indefinitely during runtime (replacing 071122 with 081122 in the url/code above will cause it to get stuck). Is there an easy way to get the program to recognize that the url for a certain date is broken (eg. https://archives.nseindia.com/archives/equities/bhavcopy/pr/PR081122.zip) and therefore ignore and continue past the try block without getting stuck?

My current workaround is to check whether a certain date is a market holiday using a DayOfWeek check as well as a HashSet containing a list of public holidays, but this is not perfect.

CodePudding user response:

So, basically your URL is returning 500 error upon requesting for invalid date. You can simply use the another method available in FileUtils

https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL, java.io.File, int, int)

Example code : (Adjust timeouts as per your requirement)

var url = new URL("https://archives.nseindia.com/archives/equities/bhavcopy/pr/PR081122.zip");
    var f = new File("NSEData.zip");
    try {
        FileUtils.copyURLToFile(url, f, 5000, 5000);
    } catch (Exception e) {
    }
  • Related