Example usage for com.google.api.client.googleapis.media MediaHttpDownloader download

List of usage examples for com.google.api.client.googleapis.media MediaHttpDownloader download

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.media MediaHttpDownloader download.

Prototype

public void download(GenericUrl requestUrl, OutputStream outputStream) throws IOException 

Source Link

Document

Executes a direct media download or a resumable media download.

Usage

From source file:GoogleDriveAPI.java

License:Apache License

/** Downloads a file using either resumable or direct media download. */
public static void download(String email, String fileID, String downFile) throws IOException {
    setup(email);/*from   www. java2 s  .co m*/
    // file path parsing
    String folderName, fileName;
    int idx = Math.max(downFile.lastIndexOf('/'), downFile.lastIndexOf('\\'));
    folderName = downFile.substring(0, idx);
    fileName = downFile.substring(idx + 1);
    // create parent directory (if necessary)
    java.io.File parentDir = new java.io.File(folderName);
    if (!parentDir.exists() && !parentDir.mkdirs()) {
        throw new IOException("Unable to create parent directory");
    }

    // OutputStream out = new FileOutputStream(new java.io.File(parentDir,
    // uploadedFile.getTitle()));
    OutputStream out = new FileOutputStream(new java.io.File(parentDir, fileName));

    File fileToDownload = drive.files().get(fileID).execute();
    MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport,
            drive.getRequestFactory().getInitializer());
    // downloader.setDirectDownloadEnabled(useDirectDownload);
    downloader.setProgressListener(new FileDownloadProgressListener());
    downloader.download(new GenericUrl(fileToDownload.getDownloadUrl()), out);
}

From source file:com.datafrog.mms.bak.DriveSample.java

License:Apache License

/** Downloads a file using either resumable or direct media download. */
private static void downloadFile(final boolean useDirectDownload, final File uploadedFile) throws IOException {
    // create parent directory (if necessary)
    java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
    if (!parentDir.exists() && !parentDir.mkdirs()) {
        throw new IOException("Unable to create parent directory");
    }/*from  w w w  .  j  av a  2s.c  om*/
    OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));

    MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport,
            drive.getRequestFactory().getInitializer());
    downloader.setDirectDownloadEnabled(useDirectDownload);
    downloader.setProgressListener(new FileDownloadProgressListener());
    System.out.println("DownLoadURL : " + uploadedFile.getDownloadUrl());
    downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}

From source file:com.github.valentin.fedoskin.fb2me.desktop.drive.DriveSample.java

License:Apache License

/** Downloads a file using either resumable or direct media download. */
private static void downloadFile(boolean useDirectDownload, File uploadedFile) throws IOException {
    // create parent directory (if necessary)
    java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
    if (!parentDir.exists() && !parentDir.mkdirs()) {
        throw new IOException("Unable to create parent directory");
    }/*from   w  w w. j  av  a  2s  .c  om*/
    OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
    Drive.Files.Get get = drive.files().get(uploadedFile.getId());
    MediaHttpDownloader downloader = get.getMediaHttpDownloader();
    downloader.setDirectDownloadEnabled(useDirectDownload);
    downloader.setProgressListener(new FileDownloadProgressListener());
    downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}

From source file:com.google.bidmanager.api.samples.GetLatestReport.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Get an authenticated connection to the API.
    DoubleClickBidManager service = SecurityUtilities.getAPIService();

    long queryId = getQueryId();
    if (queryId == 0) {
        // Call the API, getting a list of queries.
        ListQueriesResponse queryListResponse = service.queries().listqueries().execute();
        // Print them out.
        System.out.println("Id\t\tName");
        for (Query q : queryListResponse.getQueries()) {
            System.out.format("%s\t%s%n", q.getQueryId(), q.getMetadata().getTitle());
        }//from www.j av a2 s.  c  o m
    } else {
        // Call the API, getting the latest status for the passed queryId.
        Query queryResponse = service.queries().getquery(queryId).execute();

        // If it is recent enough ...
        if (queryResponse.getMetadata().getLatestReportRunTimeMs() > java.lang.System.currentTimeMillis()
                - CURRENT_REPORT_WINDOW_MS) {
            // Grab the report.
            GenericUrl reportPath = new GenericUrl(
                    queryResponse.getMetadata().getGoogleCloudStoragePathForLatestReport());
            OutputStream out = new FileOutputStream(queryResponse.getQueryId() + ".csv");
            MediaHttpDownloader downloader = new MediaHttpDownloader(SecurityUtilities.getTransport(), null);
            downloader.download(reportPath, out);
            System.out.println("Download complete.");
        } else {
            System.out.format("No reports for query Id %s in the last 12 hours.", queryId);
        }
    }
}

From source file:de.jlo.talendcomp.google.drive.DriveHelper.java

License:Apache License

private void downloadByUrl(String fileDownloadUrl, String localFilePath, boolean createDirs) throws Exception {
    checkPrerequisits();/*from w  w  w  .j ava2 s. c om*/
    lastDownloadedFilePath = null;
    lastDownloadedFileSize = 0;
    File localFile = new File(localFilePath);
    if (createDirs && localFile.getParentFile().exists() == false) {
        if (localFile.getParentFile().mkdirs() == false) {
            throw new Exception("Unable to create parent directory: " + localFile.getParent());
        }
    }
    OutputStream fileOut = new BufferedOutputStream(new FileOutputStream(localFile));
    try {
        MediaHttpDownloader downloader = new MediaHttpDownloader(HTTP_TRANSPORT,
                driveService.getRequestFactory().getInitializer());
        downloader.setDirectDownloadEnabled(false);
        downloader.setProgressListener(new MediaHttpDownloaderProgressListener() {

            @Override
            public void progressChanged(MediaHttpDownloader downloader) throws IOException {
                info("File status: " + downloader.getDownloadState());
                info("Bytes downloaded:" + downloader.getNumBytesDownloaded());
            }

        });
        downloader.download(new GenericUrl(fileDownloadUrl), fileOut);
        if (fileOut != null) {
            fileOut.flush();
            fileOut.close();
            fileOut = null;
        }
        lastDownloadedFilePath = localFile.getAbsolutePath();
        lastDownloadedFileSize = localFile.length();
    } catch (Exception e) {
        if (fileOut != null) {
            fileOut.flush();
            fileOut.close();
            fileOut = null;
        }
        Thread.sleep(200);
        if (localFile.canWrite()) {
            System.err.println("Download failed. Remove incomplete file: " + localFile.getAbsolutePath());
            localFile.delete();
        }
        throw e;
    }
}

From source file:de.pfabulist.googledrive.GoogleDriveElsewhere.java

License:BSD License

@Override
public void get(String id, long version, OutputStream os) {

    try {//from   w ww  .j av a 2s  . com
        File metadata = drive.files().get(id).execute();
        if (!metadata.getTitle().equals("" + version)) {
            Log.info("no such version");
            return;
        }
        MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport,
                drive.getRequestFactory().getInitializer());
        downloader.setDirectDownloadEnabled(true); // todo false for large files ?
        downloader.setProgressListener(new FileDownloadProgressListener());
        downloader.download(new GenericUrl(metadata.getDownloadUrl()), os);
    } catch (IOException e) {
        throw u(e);
    }
}

From source file:ifpb.pod.proj.appdata.repositorio.GDriveRepositorio.java

private java.io.File dowloadUsersFile() throws IOException, Exception, FileNotFoundException {
    File userFilesMeta = getDriveService().files().get(USRFILE).execute();
    OutputStream out = new FileOutputStream(this.getClass().getResource("/downloads/usuarios.xml").getFile());
    MediaHttpDownloader downloader = new MediaHttpDownloader(HTTP_TRANSPORT,
            getDriveService().getRequestFactory().getInitializer());
    downloader.download(new GenericUrl(userFilesMeta.getDownloadUrl()), out);
    return new java.io.File(this.getClass().getResource("/downloads/usuarios.xml").getFile());
}

From source file:ifpb.pod.proj.appdata.repositorio.GDriveRepositorio.java

private java.io.File dowloadMsgFile() throws IOException, Exception, FileNotFoundException {
    File userFilesMeta = getDriveService().files().get(MSGFILE).execute();
    OutputStream out = new FileOutputStream(this.getClass().getResource("/downloads/mensagens.xml").getFile());
    MediaHttpDownloader downloader = new MediaHttpDownloader(HTTP_TRANSPORT,
            getDriveService().getRequestFactory().getInitializer());
    downloader.download(new GenericUrl(userFilesMeta.getDownloadUrl()), out);
    return new java.io.File(this.getClass().getResource("/downloads/mensagens.xml").getFile());
}

From source file:ifpb.pod.proj.appdata.repositorio.GDriveRepositorio.java

private java.io.File dowloadUsrGpFile() throws IOException, Exception, FileNotFoundException {
    File userFilesMeta = getDriveService().files().get(USRGRP).execute();
    OutputStream out = new FileOutputStream(
            this.getClass().getResource("/downloads/usuario_grupo.xml").getFile());
    MediaHttpDownloader downloader = new MediaHttpDownloader(HTTP_TRANSPORT,
            getDriveService().getRequestFactory().getInitializer());
    downloader.download(new GenericUrl(userFilesMeta.getDownloadUrl()), out);
    return new java.io.File(this.getClass().getResource("/downloads/usuario_grupo.xml").getFile());
}

From source file:ifpb.pod.proj.appdata.repositorio.GDriveRepositorio.java

private java.io.File dowloadGpFile() throws IOException, Exception, FileNotFoundException {
    File userFilesMeta = getDriveService().files().get(GRUPOFILE).execute();
    OutputStream out = new FileOutputStream(this.getClass().getResource("/downloads/grupos.xml").getFile());
    MediaHttpDownloader downloader = new MediaHttpDownloader(HTTP_TRANSPORT,
            getDriveService().getRequestFactory().getInitializer());
    downloader.download(new GenericUrl(userFilesMeta.getDownloadUrl()), out);
    return new java.io.File(this.getClass().getResource("/downloads/grupos.xml").getFile());
}