由于我不被允许在相关帖子Save file in specific folder with Google Drive SDK
中提问File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(new File.ParentReference().setId(parentId));
File file = service.files().insert(body, textContent).execute();
文件。 “.ParentReference()”给我一个问题,我错过了什么?
我当前的代码不会加载到文件夹XXX
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);
// Create a new authorized API client
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
ParentReference newParent = new ParentReference();
newParent.setId("XXX");
// Insert a file
File body = new File();
body.setTitle(fileID);
body.setDescription(fileID);
body.setMimeType("text/plain");
body.setParents(Arrays.asList(newParent));
List<File> files = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
for (File f : files) {
System.out.println(f.getTitle() + ", " + f.getMimeType());
}
java.io.File fileContent = new java.io.File(fileID);
FileContent mediaContent = new FileContent("text/plain", fileContent);
File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());
答案 0 :(得分:1)
为文件添加父文件夹。
private static ParentReference insertFileIntoFolder(Drive service, String folderId,
String fileId) {
ParentReference newParent = new ParentReference();
newParent.setId(folderId);
try {
return service.parents().insert(fileId, newParent).execute();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
return null;
}
见