简体   繁体   中英

Set timeout on org.apache.commons.io.FileUtils.copyURLToFile?

I have some code that copies using httpCore util copyURLtoFile() but I can't seem to find anything about timeouts in the documentation like I was able to with httpClient. The file it's pulling shouldn't pull out, but depending on that is.... interesting.

    URL pjmUrl = new URL("myFile");
    File projLoad = new File("projLoad.txt");
    org.apache.commons.io.FileUtils.copyURLToFile(pjmUrl, projLoad);

If the third line should timeout, the program has no way to throw an error based on runtime or to check for threadInterrupted()

You must be looking at old javadocs. In the current release (2.4) here is an overload of the copyURLToFile method that has two timeout parameters:

  public static void copyURLToFile(URL source,
                             File destination,
                             int connectionTimeout,
                             int readTimeout)
                      throws IOException

As the javadoc explains, the time unit is milliseconds.

Reference: https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FileUtils.html

Not true, the overloaded method

FileUtils.copyURLToFile(URL source,
                        File destination,
                        int connectionTimeout,
                        int readTimeout)

does not exist in the latest 2.4. version , although the method is listed in Javadoc:

http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL,%20java.io.File,%20int,%20int)

I'm positively sure that I'm using the 2.4 version of Apache commons-io.

PS: I also downloaded source ( http://www.carfab.com/apachesoftware//commons/io/source/commons-io-2.4-src.zip ) to make sure that the above method really doesn't exist (anymore?) and inside FileUtils.java there really isn't such method.

Check your maven dependencies, there might be another older version of common-io jar in your classpath. I had the same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM