- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是一个 official example :
package com.google.api.services.samples.drive.cmdline;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.media.MediaHttpDownloader;
import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Preconditions;
import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Collections;
/**
* A sample application that runs multiple requests against the Drive API. The requests this sample
* makes are:
* <ul>
* <li>Does a resumable media upload</li>
* <li>Updates the uploaded file by renaming it</li>
* <li>Does a resumable media download</li>
* <li>Does a direct media upload</li>
* <li>Does a direct media download</li>
* </ul>
*
* @author rmistry@google.com (Ravi Mistry)
*/
public class DriveSample {
/**
* Be sure to specify the name of your application. If the application name is {@code null} or
* blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
*/
private static final String APPLICATION_NAME = "";
private static final String UPLOAD_FILE_PATH = "Enter File Path";
private static final String DIR_FOR_DOWNLOADS = "Enter Download Directory";
private static final java.io.File UPLOAD_FILE = new java.io.File(UPLOAD_FILE_PATH);
/** Directory to store user credentials. */
private static final java.io.File DATA_STORE_DIR =
new java.io.File(System.getProperty("user.home"), ".store/drive_sample");
/**
* Global instance of the {@link DataStoreFactory}. The best practice is to make it a single
* globally shared instance across your application.
*/
private static FileDataStoreFactory dataStoreFactory;
/** Global instance of the HTTP transport. */
private static HttpTransport httpTransport;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/** Global Drive API client. */
private static Drive drive;
/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(DriveSample.class.getResourceAsStream("/client_secrets.json")));
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println(
"Enter Client ID and Secret from https://code.google.com/apis/console/?api=drive "
+ "into drive-cmdline-sample/src/main/resources/client_secrets.json");
System.exit(1);
}
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton(DriveScopes.DRIVE_FILE)).setDataStoreFactory(dataStoreFactory)
.build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
public static void main(String[] args) {
Preconditions.checkArgument(
!UPLOAD_FILE_PATH.startsWith("Enter ") && !DIR_FOR_DOWNLOADS.startsWith("Enter "),
"Please enter the upload file path and download directory in %s", DriveSample.class);
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
// authorization
Credential credential = authorize();
// set up the global Drive instance
drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(
APPLICATION_NAME).build();
// run commands
View.header1("Starting Resumable Media Upload");
File uploadedFile = uploadFile(false);
View.header1("Updating Uploaded File Name");
File updatedFile = updateFileWithTestSuffix(uploadedFile.getId());
View.header1("Starting Resumable Media Download");
downloadFile(false, updatedFile);
View.header1("Starting Simple Media Upload");
uploadedFile = uploadFile(true);
View.header1("Starting Simple Media Download");
downloadFile(true, uploadedFile);
View.header1("Success!");
return;
} catch (IOException e) {
System.err.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(1);
}
/** Uploads a file using either resumable or direct media upload. */
private static File uploadFile(boolean useDirectUpload) throws IOException {
File fileMetadata = new File();
fileMetadata.setTitle(UPLOAD_FILE.getName());
FileContent mediaContent = new FileContent("image/jpeg", UPLOAD_FILE);
Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(useDirectUpload);
uploader.setProgressListener(new FileUploadProgressListener());
return insert.execute();
}
/** Updates the name of the uploaded file to have a "drivetest-" prefix. */
private static File updateFileWithTestSuffix(String id) throws IOException {
File fileMetadata = new File();
fileMetadata.setTitle("drivetest-" + UPLOAD_FILE.getName());
Drive.Files.Update update = drive.files().update(id, fileMetadata);
return update.execute();
}
/** 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");
}
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());
downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}
}
一个问题是 setTitle
方法在最新的 Java API 中不再存在。现在如何设置文件的标题?
最佳答案
上面代码的一些改动
setTitle
已更改为 setName
Drive.Files.插入
-> Drive.Files.创建
drive.files().插入
(fileMetadata, mediaContent); -> drive.files().创建
(fileMetadata,mediaContent)
getTitle()
-> getName()
uploadedFile.getDownloadUrl()
-> uploadedFile.getWebViewLink()
以下代码具有所有新的方法名称。
更改代码的最后一部分并粘贴
private static File uploadFile(boolean useDirectUpload) throws IOException {
File fileMetadata = new File();
fileMetadata.setName(UPLOAD_FILE.getName());
FileContent mediaContent = new FileContent("image/jpeg", UPLOAD_FILE);
Drive.Files.Create insert = drive.files().create(fileMetadata, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(useDirectUpload);
uploader.setProgressListener(new FileUploadProgressListener());
return insert.execute();
}
/** Updates the name of the uploaded file to have a "drivetest-" prefix. */
private static File updateFileWithTestSuffix(String id) throws IOException {
File fileMetadata = new File();
fileMetadata.setName("drivetest-" + UPLOAD_FILE.getName());
Drive.Files.Update update = drive.files().update(id, fileMetadata);
return update.execute();
}
/** 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");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getName()));
MediaHttpDownloader downloader =
new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
downloader.setDirectDownloadEnabled(useDirectDownload);
downloader.setProgressListener(new FileDownloadProgressListener());
downloader.download(new GenericUrl(uploadedFile.getWebViewLink()), out);
}
关于java - Google Drive API v3 中等效的 File.setTitle 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36485317/
我知道有两种方法可以在 Android Activity 中设置标题。 假设我已经有以下代码... @Override protected void onCreate(Bundle savedInst
我想将当前 Activity 的标题设置为相册的标题。我使用 Toolbar.setTitle() 设置标题选项。不过,我最近遇到了setTitle()它设置 Activity 名称并更改标题。 使用
我的教训是改变了JFrame的setTitle方法,所以它允许整数作为参数。怎么做?我必须重载那个方法,对吧?我在 setTitle 方法中尝试的任何操作都以堆栈溢出结束。 import javax.
当我在 WatchKit 中推送一个新 Controller 并在新 Controller 的awakeWithContext: 方法中使用 setTitle 时,它需要一秒钟左右来设置标题,直到
我目前正在开发自己的扫雷器。在重写 setTitle 方法时,我遇到以下问题。 //InfoDisplayer is inner class of class MenuActionListener
我需要通过方法(而非构造函数)设置标题。我试过这样做,但没有用: import javax.swing.*; import java.awt.*; public class PointGraphWri
我正在开发一个构建您自己的冒险应用程序,其中有 2 个按钮,每次按下它们时都会更改其 .setTitle 。我的应用程序已准备就绪 - 一切正常。每当按下按钮时,故事 textview 都会发生变化,
我正在扩展 AppCompatActivity 并使用 setSupportActionBar() 设置 ActionBar。 标题设置有点困惑。 如果我这样做 - Toolbar toolbar =
我知道这个问题已经有各种版本的堆栈溢出,但没有一个能帮助我。它只是行不通 我正在尝试以编程方式更改此处的 UIButtons 标题: override func viewDidLoad() {
我遇到过这样一种情况,在菜单管理器的 Joomla 后端指定的菜单项标题会覆盖以下调用:JFactory::getDocument()->setTitle('Custom title') 文档的标题变
我的代码在文档加载时创建一个 HighCharts 图表,然后其他代码将在用户更改某些设置时更新图表。我使用 chart.setTitle() 命令更改标题,屏幕上的图表更新得很好。 但是当图表导出为
不确定为什么会发生这种情况,但是当我尝试更改 JFrame 上的标题时它没有更新。 JFrame 是静态的,我这样调用它: Assets.frame.setTitle("test"); 我有一段代码,
我一直在修改一个应用程序,它有一个包含很多项目的 ListView 。当我长按一个项目时,它会显示一个带有重命名、删除和其他选项的菜单。这个 contextMenu 有一个标题,使用从项目中获取的字符
我在我的工具栏中使用了这两种方法: toolbar.setTitleTextColor(getResources().getColor(R.color.ColorPrimary));
我的 collapintoolbar 不是很好用.. collapsingToolbarLayout.setTitle 仅在折叠完成后起作用。我尝试在更改 fragment 时设置它。 当我更改 fr
我正在尝试根据来自 API 的 JSON 响应更新按钮的标题。 JSON 请求以及按钮标题的设置发生在我的 View Controller 中的 viewDidLoad 中,但按钮的标题直到我单击按钮
我正在使用 arc4random 创建一个测验来随机化我收到的问题。我想知道如何随机选择哪个答案出现在哪个按钮上。下面是一些代码 函数随机化(){ var RandomNumber = arc
我正在尝试添加一个数字,当您将鼠标悬停在 chrome 扩展图标上时,该数字将显示,并且该数字将随着脚本运行而改变。但是,当我添加 chrome.browserAction.setTitle({
我有一个间接继承自 MainScreen 的类,并且 setTitle(string) 不起作用。以下是我的层次结构: public class Class1 extends MainScreen {
我创建了一个滑动选项卡 Activity ,我正在尝试动态更改应用栏的标题。但无论我做什么,它似乎都不起作用!这是我的 MainActivity.java: public class MainActi
我是一名优秀的程序员,十分优秀!