我正在使用 Java Drive Rest V2 API* 进行 Google Drive Integration
,我能够获得除文档/文件路径之外的大多数文档/文件元数据属性。
*
在两个链接中,解决方案表明我必须创建一个单独的方法来满足此要求,这意味着Drive Rest V2 API没有提供直接方法来获取文件路径。
请指导我,并就此提出您的建议。
谢谢,
Arpit
分享我的解决方案,这将对其他人有所帮助:)…
经过几次Google搜索并从Google Drive文档中获得了 Java Drive Rest V2 API
,我知道没有方法可以调用/获取完整的文件路径。
因此,我创建了以下两种自定义方法来实现我的问题解决方案:
以下是代码段
public static void main(String[] args) throws IOException {
// Build a new authorized API client service.
Drive service = getDriveService();
try {
// Print the file path and name.
FileList result = service.files().list().execute();
List<File> files = result.getItems();
if (files == null || files.size() == 0) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
if (!(file.getMimeType().contains("folder"))) {
String filePath = null;
if (!(file.getParents().isEmpty())) {
filePath = getFilePath(service, file);
}
System.out.println("path: " + filePath);
System.out.println("name: " + file.getTitle());
System.out.println();
}
}
System.out.println("== END ==");
}
} catch (Exception e) {
System.out.println(e);
}
}
private static String getFilePath(Drive drive, File file) throws IOException {
String folderPath = "";
String fullFilePath = null;
List<ParentReference> parentReferencesList = file.getParents();
List<String> folderList = new ArrayList<String>();
List<String> finalFolderList = getfoldersList(drive, parentReferencesList, folderList);
Collections.reverse(finalFolderList);
for (String folder : finalFolderList) {
folderPath += "/" + folder;
}
fullFilePath = folderPath + "/" + file.getTitle();
return fullFilePath;
}
private static List<String> getfoldersList(Drive drive, List<ParentReference> parentReferencesList, List<String> folderList) throws IOException {
for (int i = 0; i < parentReferencesList.size(); i++) {
String id = parentReferencesList.get(i).getId();
File file = drive.files().get(id).execute();
folderList.add(file.getTitle());
if (!(file.getParents().isEmpty())) {
List<ParentReference> parentReferenceslist2 = file.getParents();
getfoldersList(drive, parentReferenceslist2, folderList);
}
}
return folderList;
}
-
谢谢,
Arpit Bora
问题内容: 是否有任何API可以从绝对文件路径检索文件名? 例如来自 我知道它可以像字符串操作一样工作, 但是我想知道还有没有像Java 这样的“正式”方式可以做到这一点。 NodeJS从绝对路径获取文件名? 问题答案: 使用模块的方法: 这是上面示例的文档。
我也试着做 而我不断得到这个结果。
我正试图在我的Android应用程序中导入sky.csv文件。 请帮我做这个。我试过这么多方法。然而,我没有得到任何适当的解决办法。
go代码 我把这个go编译为/usr/bin/demo Apple.java 我直接执行Apple.java,里面的go代码会被执行的,我想go获取这个Apple.java的绝对路径。 我遍历了os.Args,如果java文件是相对路径执行的,得到的就是./Apple.java,如果是绝对路径执行的,能获取到。
问题内容: 给定路径,例如,我如何找到相对于Python中当前工作目录的文件的绝对路径?例如在Windows上,我可能会得到以下结果: 问题答案: 如果已经是绝对路径,也可以使用: