Class FileDownloadUtils


  • public class FileDownloadUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copy​(java.io.File src, java.io.File dst)
      Copy the content of file src to dst TODO since java 1.7 this is provided in java.nio.file.Files
      static void deleteDirectory​(java.lang.String dir)
      Recursively delete a folder & contents
      static void deleteDirectory​(java.nio.file.Path dir)
      Recursively delete a folder & contents
      static void downloadFile​(java.net.URL url, java.io.File destination)
      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
      static java.lang.String expandUserHome​(java.lang.String file)
      Expands ~ in paths to the user's home directory.
      static java.lang.String getFileExtension​(java.io.File f)  
      static java.lang.String getFilePrefix​(java.io.File f)  
      static void main​(java.lang.String[] args)  
      static boolean ping​(java.lang.String url, int timeout)
      Pings a HTTP URL.
      static java.net.URLConnection prepareURLConnection​(java.lang.String url, int timeout)
      Prepare URLConnection with customised timeouts.
      static java.lang.String toUnixPath​(java.lang.String path)
      Converts path to Unix convention and adds a terminating slash if it was omitted
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileDownloadUtils

        public FileDownloadUtils()
    • Method Detail

      • copy

        public static void copy​(java.io.File src,
                                java.io.File dst)
                         throws java.io.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:
        java.io.IOException
      • getFileExtension

        public static java.lang.String getFileExtension​(java.io.File f)
      • getFilePrefix

        public static java.lang.String getFilePrefix​(java.io.File f)
      • downloadFile

        public static void downloadFile​(java.net.URL url,
                                        java.io.File destination)
                                 throws java.io.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
        Parameters:
        url -
        destination -
        Throws:
        java.io.IOException
      • toUnixPath

        public static java.lang.String toUnixPath​(java.lang.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 java.lang.String expandUserHome​(java.lang.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 (/.../~/...)

        Parameters:
        file -
        Returns:
      • ping

        public static boolean ping​(java.lang.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 java.net.URLConnection prepareURLConnection​(java.lang.String url,
                                                                  int timeout)
                                                           throws java.io.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:
        java.io.IOException
      • deleteDirectory

        public static void deleteDirectory​(java.nio.file.Path dir)
                                    throws java.io.IOException
        Recursively delete a folder & contents
        Parameters:
        dir - directory to delete
        Throws:
        java.io.IOException
      • deleteDirectory

        public static void deleteDirectory​(java.lang.String dir)
                                    throws java.io.IOException
        Recursively delete a folder & contents
        Parameters:
        dir - directory to delete
        Throws:
        java.io.IOException
      • main

        public static void main​(java.lang.String[] args)