本文整理汇总了Java中com.google.api.services.drive.Drive.Files类的典型用法代码示例。如果您正苦于以下问题:Java Files类的具体用法?Java Files怎么用?Java Files使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Files类属于com.google.api.services.drive.Drive包,在下文中一共展示了Files类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFiles
import com.google.api.services.drive.Drive.Files; //导入依赖的package包/类
/**
* Gets all the files from a request.
*
* @param request the request
* @param excludeSharedWithMe true to exclude shared with me files
* @return a map of file id to file
*/
private Map<String, File> getFiles(Files.List request, boolean excludeSharedWithMe)
throws IOException {
Map<String, File> idToFileMap = new HashMap<String, File>();
do {
FileList files = request.execute();
for (File file : files.getItems()) {
if (excludeSharedWithMe && file.getSharedWithMeDate() != null) {
continue;
}
idToFileMap.put(file.getId(), file);
}
request.setPageToken(files.getNextPageToken());
} while (request.getPageToken() != null && request.getPageToken().length() > 0);
return idToFileMap;
}
示例2: deleteCredentials
import com.google.api.services.drive.Drive.Files; //导入依赖的package包/类
public void deleteCredentials() throws IOException {
if (getState() != State.disconnected) {
throw new IOException("Cannot delete file store while connected");
}
synchronized (this) {
this.userInfo = null;
}
File file = new File(dataStoreDir, StoredCredential.DEFAULT_DATA_STORE_ID);
if (file.exists()) {
java.nio.file.Files.delete(file.toPath());
}
File userInfoFile = new File(dataStoreDir, GDriveConnectionManager.USER_FILENAME);
if (userInfoFile.exists()) {
java.nio.file.Files.delete(userInfoFile.toPath());
}
}
示例3: uploadFile
import com.google.api.services.drive.Drive.Files; //导入依赖的package包/类
public static com.google.api.services.drive.model.File uploadFile(
java.io.File file, Credential credential, String parent)
throws IOException {
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setParents(Arrays.asList(new ParentReference()
.setId(parent)));
fileMetadata.setTitle(file.getName());
InputStreamContent mediaContent = new InputStreamContent(
"image/png", new BufferedInputStream(new FileInputStream(
file)));
mediaContent.setLength(file.length());
Drive.Files.Insert insert = drive.files().insert(fileMetadata,
mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(true);
return insert.execute();
}
示例4: uploadFile
import com.google.api.services.drive.Drive.Files; //导入依赖的package包/类
public File uploadFile(String name, String mimeType, java.io.File mediaFile, boolean onWifi)
throws IOException, JSONException {
// File Metadata
File fileMetadata = new File();
fileMetadata.setTitle(name);
fileMetadata.setMimeType(mimeType);
// Set the parent folder.
ParentReference uploadDir = new ParentReference();
uploadDir.setId(findUploadDirectory().getId());
fileMetadata.setParents(Arrays.asList(uploadDir));
InputStreamContent mediaContent = new InputStreamContent(mimeType, new BufferedInputStream(
new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent);
insert.getMediaHttpUploader().setProgressListener(new ProgressListener(mediaFile));
insert.getMediaHttpUploader().setBackOffPolicyEnabled(true);
int chunkSize = onWifi ? MediaHttpUploader.MINIMUM_CHUNK_SIZE * 2
: MediaHttpUploader.MINIMUM_CHUNK_SIZE;
insert.getMediaHttpUploader().setChunkSize(chunkSize);
return insert.execute();
}
示例5: touchFile
import com.google.api.services.drive.Drive.Files; //导入依赖的package包/类
private File touchFile(String fileId, File patch, int retry) {
try {
Files.Patch patchRequest = drive.files().patch(fileId, patch);
if (patch.getModifiedDate() != null) {
patchRequest.setSetModifiedDate(true);
}
// control we are not exceeding number of requests/second
bandwidthController.newRequest();
File updatedFile = patchRequest.execute();
return updatedFile;
} catch (Exception e) {
if (retry > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
logger.info("retrying...");
touchFile(fileId, patch, --retry);
}
throw new RuntimeException(e);
}
}
示例6: putResource
import com.google.api.services.drive.Drive.Files; //导入依赖的package包/类
public File putResource(GoogleDrivePutParameters parameters) throws IOException {
String folderId = parameters.getDestinationFolderId();
File putFile = new File();
putFile.setParents(Collections.singletonList(folderId));
Files.List fileRequest = drive.files().list()
.setQ(format(QUERY_NOTTRASHED_NAME_NOTMIME_INPARENTS, parameters.getResourceName(), MIME_TYPE_FOLDER, folderId));
LOG.debug("[putResource] `{}` Exists in `{}` ? with `{}`.", parameters.getResourceName(),
parameters.getDestinationFolderId(), fileRequest.getQ());
FileList existingFiles = fileRequest.execute();
if (existingFiles.getFiles().size() > 1) {
throw new IOException(messages.getMessage("error.file.more.than.one", parameters.getResourceName()));
}
if (existingFiles.getFiles().size() == 1) {
if (!parameters.isOverwriteIfExist()) {
throw new IOException(messages.getMessage("error.file.already.exist", parameters.getResourceName()));
}
LOG.debug("[putResource] {} will be overwritten...", parameters.getResourceName());
drive.files().delete(existingFiles.getFiles().get(0).getId()).execute();
}
putFile.setName(parameters.getResourceName());
String metadata = "id,parents,name";
if (!StringUtils.isEmpty(parameters.getFromLocalFilePath())) {
// Reading content from local fileName
FileContent fContent = new FileContent(null, new java.io.File(parameters.getFromLocalFilePath()));
putFile = drive.files().create(putFile, fContent).setFields(metadata).execute();
//
} else if (parameters.getFromBytes() != null) {
AbstractInputStreamContent content = new ByteArrayContent(null, parameters.getFromBytes());
putFile = drive.files().create(putFile, content).setFields(metadata).execute();
}
return putFile;
}
示例7: performInitialSync
import com.google.api.services.drive.Drive.Files; //导入依赖的package包/类
/**
* Performs initial sync.
*/
private void performInitialSync() throws IOException {
// Get the largest change id first to avoid race conditions
About about = drive.about().get().setFields(ABOUT_GET_FIELDS).execute();
long largestChangeId = about.getLargestChangeId();
// Get all the KML/KMZ files in the "My Drive:/My Tracks" folder
Files.List myTracksFolderRequest = drive.files()
.list().setQ(String.format(Locale.US, SyncUtils.MY_TRACKS_FOLDER_FILES_QUERY, folderId));
Map<String, File> myTracksFolderMap = getFiles(myTracksFolderRequest, true);
// Handle tracks that are already uploaded to Google Drive
Set<String> syncedDriveIds = updateSyncedTracks();
for (String driveId : syncedDriveIds) {
myTracksFolderMap.remove(driveId);
}
// Get all the KML/KMZ files in the "Shared with me:/" folder
Files.List sharedWithMeRequest = drive.files()
.list().setQ(SyncUtils.SHARED_WITH_ME_FILES_QUERY);
Map<String, File> sharedWithMeMap = getFiles(sharedWithMeRequest, false);
try {
insertNewTracks(myTracksFolderMap.values());
insertNewTracks(sharedWithMeMap.values());
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
} catch (IOException e) {
// Remove all imported tracks
Cursor cursor = null;
try {
cursor = myTracksProviderUtils.getTrackCursor(SyncUtils.DRIVE_ID_TRACKS_QUERY, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
Track track = myTracksProviderUtils.createTrack(cursor);
if (!syncedDriveIds.contains(track.getDriveId())) {
myTracksProviderUtils.deleteTrack(context, track.getId());
}
} while (cursor.moveToNext());
}
} finally {
if (cursor != null) {
cursor.close();
}
}
throw e;
}
}
示例8: setUp
import com.google.api.services.drive.Drive.Files; //导入依赖的package包/类
private void setUp() {
client = mock(Drive.class);
files = mock(Files.class);
when(client.files()).thenReturn(files);
}