image thumbnail

Java based Matlab file downloader

by

 

This Java based Matlab file downloader supports proxy, proxy testing, https, http forwarding

downloadUrl(link, filePath, skipExistingFile, retryLimit, proxy_address, proxy_port)
function javafileName = downloadUrl(link, filePath, skipExistingFile, retryLimit, proxy_address, proxy_port)
    % link the url that to be downloaded e.g., 'http://download.tuxfamily.org/notepadplus/6.3.3/npp.6.3.3.Installer.exe'
    % filePath the path in which the downloaded file will be save. e.g., './'
    % skipExistingFile (true or false). skip downloading if the file exists in the current path. false by defualt.
    % retryLimit # of times to retry  e.g., 0 by default
    % optional: set the proxy_address
    % optional: set the proxy_port.
    if(~exist('retryLimit', 'var') || isempty(retryLimit))
        retryLimit = 0;
    end
    if(~exist('skipExistingFile', 'var') || isempty(skipExistingFile))
        skipExistingFile = false;
    end 
    fid = fopen('dowload_log.txt', 'a+');
    warning off;
    javaaddpath('./HttpsDownloader/dist/HttpsDownloader.jar');
    warning on;
    
    httpsdownloader.HttpsSetup.setupHttps();
    if(exist('proxy_port', 'var') && exist('proxy_port', 'var') && ~isempty(proxy_port) && ~isempty(proxy_address))
        if(httpsdownloader.HttpsDownloader.proxyTest('http://www.google.com', proxy_address, proxy_port))
            javaDownloader = httpsdownloader.HttpsDownloader(link, filePath, [], [], httpsdownloader.HttpsDownloader.setproxy(proxy_address, proxy_port));
        else
            fprintf('proxy error\n');
            javafileName = [];
            return;
        end
    else
        javaDownloader = httpsdownloader.HttpsDownloader(link, filePath, [], [], []);
    end
    retryCount = 0;
    while(javaDownloader.connInf.fileSize <= 0)
        msg = sprintf('File is not available online (retry %d) %s', retryCount, link);
        fprintf(msg);
        fprintf(fid, '%s, %s, File is not available online (retry %d) \n', datestr(now,'yyyy-mmm-dd HH:MM:SS'), link, retryCount);        
        if(retryCount >= retryLimit)
            fprintf('\n');
            fclose(fid);
            javafileName = 'File is not available online';
            return;
        else            
            retryCount = retryCount + 1;
            if(exist('proxy_port', 'var') && exist('proxy_port', 'var') && ~isempty(proxy_port) && ~isempty(proxy_address))
                javaDownloader = httpsdownloader.HttpsDownloader(link, filePath, [], [], httpsdownloader.HttpsDownloader.setproxy(proxy_address, proxy_port));
            else
                javaDownloader = httpsdownloader.HttpsDownloader(link, filePath, [], [], []);
            end
            fprintf(repmat('\b', 1, numel(msg)));
        end
    end
    if(exist(char(javaDownloader.connInf.fileName), 'file') && skipExistingFile)
        fprintf('Skip download, since the file exists %s\n', char(javaDownloader.connInf.fileName));
        fprintf(fid, '%s, %s, %s, Skip download, since the file exists \n', datestr(now,'yyyy-mmm-dd HH:MM:SS'), link, char(javaDownloader.connInf.fileName));
        fclose(fid);
        javafileName = char(javaDownloader.connInf.fileName);
        return;
    end
    count = 0;
    tic;
    fprintf('Downloading %s :', char(javaDownloader.connInf.fileName));
    fprintf(fid, '%s, %s, Downloading %s :', datestr(now,'yyyy-mmm-dd HH:MM:SS'), link, char(javaDownloader.connInf.fileName));
    fileSizeInMB = javaDownloader.connInf.fileSize/1024/1024;
    h = tic;
    try
        while true
            t = toc(h);
            downloadedSizeInMB = javaDownloader.downloadedSize/1048576;
            speedInMB = downloadedSizeInMB/t;
            str = sprintf('%2.4f MB/s %5.3f MB of %5.3f (in %4.1f s)', speedInMB, downloadedSizeInMB, fileSizeInMB, (fileSizeInMB - downloadedSizeInMB)/speedInMB);
            fprintf(1,'%s', str);
            n = javaDownloader.downloadwithProgress();
            fprintf(1, repmat('\b',1, numel(str)));
            count = count + 1;
            if (n <0)
                break;
            end
        end
    catch err
    end
    fprintf(' done in %4.2f s \n', toc(h));
    fprintf(fid, ' done in %4.2f s \n', toc(h));
    fclose(fid);
    javafileName = char(javaDownloader.connInf.fileName);
end

Contact us