List of usage examples for com.google.api.client.googleapis.media MediaHttpDownloader getNumBytesDownloaded
public long getNumBytesDownloaded()
From source file:com.jiubang.go.backup.pro.net.sync.FileDownloadProgressListener.java
License:Apache License
@Override public void progressChanged(MediaHttpDownloader downloader) { switch (downloader.getDownloadState()) { case MEDIA_IN_PROGRESS: if (mListener != null) { mListener.onProgress(downloader.getNumBytesDownloaded(), mFile.getFileSize(), null); }//from w ww .j a v a 2 s . c o m break; case MEDIA_COMPLETE: break; } }
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 . ja v a 2 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; } }