如何使用 Java Drive Rest V2 API 从 Google Drive 获取文档和文件的绝对路径? [英] How to get absolute path of a document and file from Google Drive Using Java Drive Rest V2 API?

查看:30
本文介绍了如何使用 Java Drive Rest V2 API 从 Google Drive 获取文档和文件的绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java Drive Rest V2 API 进行 Google Drive 集成,我能够获得大部分文档/文件元数据属性,但路径除外文档/文件.

I'm working on Google Drive Integration using Java Drive Rest V2 API, I'm able to get most of the document/file metadata properties except path of the document/file.

我还提到了以下 StackOverflow 问题:

I referred following StackOverflow questions as well:

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

In Both links solutions indicate that I have to create a separate method to achieve this requirement, It means there is no direct method provided by Drive Rest V2 API to get file path.

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

Please guide me and suggest your suggestion on this.

谢谢,

阿皮特

推荐答案

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

Sharing my solution which will be helpful to others :)...

经过几次 google 搜索和 Google Drive Documentation for Java Drive Rest V2 API,我知道没有方法可以调用/获取完整的文件路径.

After several google searches and from Google Drive Documentation for Java Drive Rest V2 API, I got to know that there is no method to call/get the full file path.

所以我创建了以下两个自定义方法来实现我的问题解决方案:

So I created following two custom method to achieve my question solution:

  1. "getFilePath(drive, file)" 返回类型为字符串.
  2. "getfoldersList(drive, parentReferencesList, folderList)" 返回类型为字符串的列表.
  1. "getFilePath(drive, file)" with String return type.
  2. "getfoldersList(drive, parentReferencesList, folderList)" with List of String return type.

下面是代码片段

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;
}

--

谢谢,

阿尔皮波拉

这篇关于如何使用 Java Drive Rest V2 API 从 Google Drive 获取文档和文件的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆