使用Google Drive SDK Java将文件保存在特定的文件夹中 [英] Save file in specific folder with Google Drive SDK Java
本文介绍了使用Google Drive SDK Java将文件保存在特定的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Since I was not allowed to ask in the relevant thread 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()"给我一个问题,我想念什么?
The File. ".ParentReference()" is giving me a problem what am I missing?
我当前的代码将不会加载到文件夹XXX
My current code which will not load into folder 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());
推荐答案
为文件添加父文件夹.
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;
}
请参见
这篇关于使用Google Drive SDK Java将文件保存在特定的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文