Cod*_*eji 5 java google-drive-api
我试过下面的代码......没有任何运气......
private void updateFile(Drive service, String fileId) {
try {
File file = new File(); /********/
final java.io.File fileToUpdate = new java.io.File("D:/Work Data/Files/pdf.pdf");
FileContent mediaContent = new FileContent("image/pdf", fileToUpdate);
file = service.files().update(fileId, file, mediaContent).execute();
System.out.println(fileId);
} catch (Exception e) {
if (isDebug) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
也试过...
private void updateFile(Drive service, String fileId) {
try {
File file = service.files().get(fileId).execute(); /********/
final java.io.File fileToUpdate = new java.io.File("D:/Work Data/Files/pdf.pdf");
FileContent mediaContent = new FileContent("image/pdf", fileToUpdate);
file = service.files().update(fileId, file, mediaContent).execute();
System.out.println(fileId);
} catch (Exception e) {
if (isDebug) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
每次我执行代码时,我都会得到以下堆栈跟踪:
java.lang.IllegalArgumentException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
at com.google.api.client.googleapis.media.MediaHttpUploader.setInitiationRequestMethod(MediaHttpUploader.java:872)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.initializeMediaUpload(AbstractGoogleClientRequest.java:237)
at com.google.api.services.drive.Drive$Files$Update.<init>(Drive.java:3163)
at com.google.api.services.drive.Drive$Files.update(Drive.java:3113)
at com.test.DriveTester.updateFile(DriveTester.java:76)
at com.test.DriveTester.main(DriveTester.java:64)
Run Code Online (Sandbox Code Playgroud)
谁能告诉我做错了什么?任何示例代码,即更新谷歌驱动器上现有文件的内容都会有所帮助......
小智 6
不幸的是,对于 API v3,Android Enthusiast 提出的解决方案不起作用。问题在于这一点:
// First retrieve the file from the API.
File file = service.files().get(fileId).execute();
Run Code Online (Sandbox Code Playgroud)
这样做它会创建一个 File 对象,它的 ID 字段被设置,当执行更新时,它会抛出异常,因为 ID 元字段不能直接编辑。
您可以做的只是创建一个新文件:
File file = new File();
Run Code Online (Sandbox Code Playgroud)
如示例所示,更改您想要的元数据并根据需要更新文件内容。
然后只需按照上面的建议更新文件:
// Send the request to the API.
File updatedFile = service.files().update(fileId, file, mediaContent).execute();
Run Code Online (Sandbox Code Playgroud)
因此,基于 Android Enthusiast 解决方案的完整示例如下所示:
private static File updateFile(Drive service, String fileId, String newTitle,
String newDescription, String newMimeType, String newFilename, boolean newRevision) {
try {
// First create a new File.
File file = new File();
// File's new metadata.
file.setTitle(newTitle);
file.setDescription(newDescription);
file.setMimeType(newMimeType);
// File's new content.
java.io.File fileContent = new java.io.File(newFilename);
FileContent mediaContent = new FileContent(newMimeType, fileContent);
// Send the request to the API.
File updatedFile = service.files().update(fileId, file, mediaContent).execute();
return updatedFile;
} catch (IOException e) {
System.out.println("An error occurred: " + e);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
要更新文件内容,您可以使用Files:update,此方法支持 /upload URI 并接受具有以下特征的上传媒体:
此方法通过两个单独的 URI 提供媒体上传功能。有关更多详细信息,请参阅媒体上传文档。
PUT https://www.googleapis.com/upload/drive/v2/files/fileId * 元数据 URI,用于仅元数据请求:
private static File updateFile(Drive service, String fileId, String newTitle,
String newDescription, String newMimeType, String newFilename, boolean newRevision) {
try {
// First retrieve the file from the API.
File file = service.files().get(fileId).execute();
// File's new metadata.
file.setTitle(newTitle);
file.setDescription(newDescription);
file.setMimeType(newMimeType);
// File's new content.
java.io.File fileContent = new java.io.File(newFilename);
FileContent mediaContent = new FileContent(newMimeType, fileContent);
// Send the request to the API.
File updatedFile = service.files().update(fileId, file, mediaContent).execute();
return updatedFile;
} catch (IOException e) {
System.out.println("An error occurred: " + e);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以查看此SO 票证,该票证讨论了上述错误。
| 归档时间: |
|
| 查看次数: |
13939 次 |
| 最近记录: |