栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Java Drive Rest V2 API从Google Drive获取文档和文件的绝对路径?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Java Drive Rest V2 API从Google Drive获取文档和文件的绝对路径?

分享我的解决方案,这将对其他人有所帮助:)…

经过几次Google搜索并从Google Drive文档中获得了 Java Drive Rest V2 API
,我知道没有方法可以调用/获取完整的文件路径。

因此,我创建了以下两种自定义方法来实现我的问题解决方案:

  1. 具有字符串返回类型的“ getFilePath(drive,file) ”。
  2. getfoldersList(drive,parentReferencesList,folderList) ”与字符串返回类型的列表。

以下是代码段

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428113.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号