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

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

问题描述

我正在使用 Java Drive Rest V2 API 开发 Google云端硬盘集成,我可以获取大部分文档/文件元数据属性, / b>



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



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



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



经过多次Google搜索以及来自Google Drive文档的 Java Drive Rest V2 API ,我所以我创建了以下两个自定义方法来实现我的问题解决方案:$ / $> $ / $> $ / $> b
$ b


  1. getFilePath(drive,file)带有字符串返回类型。使用String返回类型列表的 getfoldersList(drive,parentReferencesList,folderList)


$ b 以下是代码片段

  public static void main(String [] args)throws IOException {
//构建一个新的授权API客户端服务。
Drive service = getDriveService();
尝试{
//打印文件路径和名称。
FileList result = service.files()。list()。execute();
列表< File> files = result.getItems();

if(files == null || files.size()== 0){
System.out.println(No files found。);
} else {
System.out.println(Files:); (文件文件:文件){
if(!(file.getMimeType()。contains(folder))){
String filePath = null;

(!(file.getParents()。isEmpty())){
filePath = getFilePath(service,file);

if
}

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 =;
字符串fullFilePath = null;

List< ParentReference> parentReferencesList = file.getParents();
列表< String> folderList = new ArrayList< String>();

列表< String> finalFolderList = getfoldersList(drive,parentReferencesList,folderList);
Collections.reverse(finalFolderList);

for(String folder:finalFolderList){
folderPath + =/+ folder;
}

fullFilePath = folderPath +/+ file.getTitle();

返回fullFilePath;
}

private static List< String> getfoldersList(Drive drive,List< ParentReference> parentReferencesList,List< String> folderList)抛出IOException { parentReferencesList.get(ⅰ).getId();

文件file = drive.files()。get(id).execute();
folderList.add(file.getTitle()); $!
$ b $ if(!(file.getParents()。isEmpty())){
List< ParentReference> parentReferenceslist2 = file.getParents();
getfoldersList(drive,parentReferenceslist2,folderList);
}
}
return folderList;
}

-



谢谢,



Arpit Bora


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.

I referred following StackOverflow questions as well:

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.

Thank you,

Arpit

解决方案

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

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)" with String return type.
  2. "getfoldersList(drive, parentReferencesList, folderList)" with List of String return type.

Below is the Code Snippet

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

--

Thanks,

Arpit Bora

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

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