public static void downloadFileFromUrl(URL url, String localFile, String userAgent) throws IOException
    {
        InputStream is = null;
        FileOutputStream fstream = null;

        try
        {
            URLConnection urlConn = url.openConnection();

            if (userAgent != null)
            {
                urlConn.setRequestProperty("User-Agent", userAgent);
            }
            urlConn.connect();
            
            long fileSize = urlConn.getContentLength();
            if ( fileSize != -1 )
            {
                System.out.println("Filesize: " + fileSize);
            }

            is = urlConn.getInputStream();
            fstream = new FileOutputStream(localFile);

            byte[] buffer = new byte[4096];
            int len;
            long bytes = 0;
            long p = 0;
            long op = -1;
            while ((len = is.read(buffer)) > 0)
            {
                fstream.write(buffer, 0, len);
                
                if ( reportDownload && fileSize != -1 )
                {
                    bytes += len;
                    p = bytes * 100 / fileSize;
                    
                    if ( op != p )
                    {
                        System.out.println("Downloaded " + p + "%");
                        op = p;
                    }
                }
            }
        }
        finally
        {
            try
            {
                if (is != null)
                {
                    is.close();
                }
            }
            finally
            {
                if (fstream != null)
                {
                    fstream.close();
                }
            }
        }
    }