Android URL Download downloadFile(String downloadUrl, String outputFilePath)

Here you can find the source of downloadFile(String downloadUrl, String outputFilePath)

Description

Check file download from url.

License

Open Source License

Parameter

Parameter Description
downloadUrl - url of file to download
outputFilePath - file path for output

Declaration

public static void downloadFile(String downloadUrl,
        String outputFilePath) throws Exception 

Method Source Code

/****************************************************************************
 * Copyright (C) 2014 GGA Software Services LLC
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 3 as published by the Free Software
 * Foundation.//from  w w  w.ja  v a 2 s  . c  o  m
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 ***************************************************************************/

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.openqa.selenium.Cookie;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Set;

public class Main{
    /**
     * Check file download from url.
     *
     * @param downloadUrl     - url of file to download
     * @param outputFilePath  - file path for output
     */
    public static void downloadFile(String downloadUrl,
            String outputFilePath) throws Exception {

        ReporterNGExt.logAction("", "",
                String.format("Download file form url: %s", downloadUrl));

        CookieStore cookieStore = seleniumCookiesToCookieStore();
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.setCookieStore(cookieStore);
        HttpGet httpGet = new HttpGet(downloadUrl);
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            File outputFile = new File(outputFilePath);
            InputStream inputStream = entity.getContent();
            FileOutputStream fileOutputStream = new FileOutputStream(
                    outputFile);
            int read;
            byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, read);
            }
            fileOutputStream.close();
            ReporterNGExt.logTechnical(String.format(
                    "downloadFile: %d bytes. %s", outputFile.length(),
                    entity.getContentType()));
        } else {
            ReporterNGExt.logFailedScreenshot(ReporterNGExt.BUSINESS_LEVEL,
                    "Download failed!");
        }
    }
    /**
     * Get Cookie from WebDriver browser session.
     *
     * @return cookieStore from WebDriver browser session.
     */
    private static CookieStore seleniumCookiesToCookieStore() {

        Set<Cookie> seleniumCookies = WebDriverWrapper.getDriver().manage()
                .getCookies();
        CookieStore cookieStore = new BasicCookieStore();

        for (Cookie seleniumCookie : seleniumCookies) {
            BasicClientCookie basicClientCookie = new BasicClientCookie(
                    seleniumCookie.getName(), seleniumCookie.getValue());
            basicClientCookie.setDomain(seleniumCookie.getDomain());
            basicClientCookie.setExpiryDate(seleniumCookie.getExpiry());
            basicClientCookie.setPath(seleniumCookie.getPath());
            cookieStore.addCookie(basicClientCookie);
        }

        return cookieStore;
    }
}

Related

  1. download(String url, String directoryName, String fileName)
  2. downloadFile(URL url, OutputStream output)
  3. downloadHtmlPage(String pageUrl)
  4. downloadUrl(String urlString)
  5. getJSONResponseFromURL(String url, Hashtable httpGetParams)