java - 使用 Google Drive SDK Java 将文件保存在特定文件夹中

标签 java google-drive-api

因为我不被允许在相关线程中询问Save file in specific folder with Google Drive SDK

File body = new File();  
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(new File.ParentReference().setId(parentId));  
File file = service.files().insert(body, textContent).execute();

文件。 “.ParentReference()”给我带来了一个问题,我错过了什么?

我当前的代码不会加载到文件夹 XXX

      HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
        GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

//      Create a new authorized API client
        Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

        ParentReference newParent = new ParentReference();
        newParent.setId("XXX");

//      Insert a file  
        File body = new File();
        body.setTitle(fileID);
        body.setDescription(fileID);
        body.setMimeType("text/plain");
        body.setParents(Arrays.asList(newParent));

        List<File> files = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
        for (File f : files) {
            System.out.println(f.getTitle() + ", " + f.getMimeType());
        }

        java.io.File fileContent = new java.io.File(fileID);
        FileContent mediaContent = new FileContent("text/plain", fileContent);

        File file = service.files().insert(body, mediaContent).execute();
        System.out.println("File ID: " + file.getId());

最佳答案

添加文件的父文件夹。

  private static ParentReference insertFileIntoFolder(Drive service, String folderId,
      String fileId) {
    ParentReference newParent = new ParentReference();
    newParent.setId(folderId);
    try {
      return service.parents().insert(fileId, newParent).execute();
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
    }
    return null;
  }

参见

关于java - 使用 Google Drive SDK Java 将文件保存在特定文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21358723/

相关文章:

java - 在 SmartGWT ListGrid 中获取可见列

java - 保护实体免受 Hibernate 中的级联删除

php - Google drive api 将共享文件夹添加到我的文件

python - 如何在colab中打开Goog​​le Drive中的json文件

javascript - 在我所有的执行正确之前调用 gapi.client.load(...)?

google-drive-api - 在 PHP 网络应用程序中使用 Google Picker 访问共享的 Google Drive

java - 原子整数的compareandexchange()与compareandset()

java - 将文件中每个 int 的值分配给 string

java - ClassNotFoundException 我没有成功捕获

android - 无法使用 Google Drive Android API 创建电子表格文件