Class FileDownloadUtils

    • Method Detail

      • copy

        public static void copy​(File src,
                                File dst)
                         throws IOException
        Copy the content of file src to dst TODO since java 1.7 this is provided in java.nio.file.Files
        Parameters:
        src -
        dst -
        Throws:
        IOException
      • getFileExtension

        public static String getFileExtension​(File f)
        Gets the file extension of a file, excluding '.'. If the file name has no extension the file name is returned.
        Parameters:
        f - a File
        Returns:
        The extension
      • getFilePrefix

        public static String getFilePrefix​(File f)
        Gets the file name up to and excluding the first '.' character. If there is no extension, the full filename is returned.
        Parameters:
        f - A file
        Returns:
        A possibly empty but non-null String.
      • downloadFile

        public static void downloadFile​(URL url,
                                        File destination)
                                 throws IOException
        Download the content provided at URL url and store the result to a local file, using a temp file to cache the content in case something goes wrong in download. A timeout of 60 seconds is hard-coded and 10 retries are attempted.
        Parameters:
        url -
        destination -
        Throws:
        IOException
      • toUnixPath

        public static String toUnixPath​(String path)
        Converts path to Unix convention and adds a terminating slash if it was omitted.
        Parameters:
        path - original platform dependent path
        Returns:
        path in Unix convention
        Since:
        3.2
      • expandUserHome

        public static String expandUserHome​(String file)
        Expands ~ in paths to the user's home directory.

        This does not work for some special cases for paths: Other users' homes (~user/...), and Tilde expansion within the path (/.../~/...). In these cases the original argument is returned.

        Parameters:
        file - A filepath starting with a tilde
        Returns:
        An absolute path
      • ping

        public static boolean ping​(String url,
                                   int timeout)
        Pings a HTTP URL. This effectively sends a HEAD request and returns true if the response code is in the 200-399 range.
        Parameters:
        url - The HTTP URL to be pinged.
        timeout - The timeout in millis for both the connection timeout and the response read timeout. Note that the total timeout is effectively two times the given timeout.
        Returns:
        true if the given HTTP URL has returned response code 200-399 on a HEAD request within the given timeout, otherwise false.
      • prepareURLConnection

        public static URLConnection prepareURLConnection​(String url,
                                                         int timeout)
                                                  throws IOException
        Prepare URLConnection with customised timeouts.
        Parameters:
        url - The URL
        timeout - The timeout in millis for both the connection timeout and the response read timeout. Note that the total timeout is effectively two times the given timeout.

        Example of code. UrlConnection conn = prepareURLConnection("http://www.google.com/", 20000); conn.connect(); conn.getInputStream();

        NB. User should execute connect() method before getting input stream.

        Returns:
        Throws:
        IOException
      • main

        public static void main​(String[] args)