【问题标题】:How to get absolute path of a document and file from Google Drive Using Java Drive Rest V2 API?如何使用 Java Drive Rest V2 API 从 Google Drive 获取文档和文件的绝对路径?
【发布时间】:2017-03-24 18:07:05
【问题描述】:

我正在使用 Java Drive Rest V2 API 开发 Google Drive Integration,我能够获取除路径之外的大部分文档/文件元数据属性文件/文件。

我还提到了以下 StackOverflow 问题:

在两个链接解决方案中都表明我必须创建一个单独的方法来实现这个要求,这意味着 Drive Rest V2 API 没有提供直接的方法来获取文件路径。

请指导我并就此提出您的建议。

谢谢,

阿皮特

【问题讨论】:

  • google drive.files 中没有这样的路径概念
  • 嗨,@ZigMandel 感谢您的回复。这意味着我必须编写一个像“getFilePath(service,file)”这样的自定义方法并将文件迭代到它的超级父级 [使用 file.getParents()] 来实现这个要求。

标签: java rest api google-drive-api


【解决方案1】:

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

在谷歌搜索和从谷歌驱动文档中获得 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;
}

--

谢谢,

阿皮特波拉

【讨论】:

    【解决方案2】:

    尝试使用Parents.get。成功的请求会返回到父文件和文件本身的链接。这是一个示例响应:

    {
     "kind": "drive#parentReference",
     "id": "0Bw-w9jw2bwglbzlvMVh2cll2dmM",
     "selfLink": "https://www.googleapis.com/drive/v2/files/1-ABCDEzyvp9NFmWz7h6ssgkTKHytO1Nq4SNIboDW8A/parents/0Bw-w9jw2bwglbzlvMVh2cll2dmM",
     "parentLink": "https://www.googleapis.com/drive/v2/files/ABCDEjw2bwglbzlvMVh2cll2dmM",
     "isRoot": false
    }
    

    这是文档中的 JAVA 实现:

     private static boolean isFileInFolder(Drive service, String folderId,
          String fileId) throws IOException {
        try {
          service.parents().get(fileId, folderId).execute();
        } catch (HttpResponseException e) {
          if (e.getStatusCode() == 404) {
            return false;
          } else {
            System.out.println("An error occured: " + e);
            throw e;
          }
        } catch (IOException e) {
          System.out.println("An error occured: " + e);
          throw e;
        }
        return true;
      }
    

    【讨论】:

    • 感谢@noogui 的回复,您的建议使我达到了我的要求。我会尽快发布我的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多