繁体   English   中英

Google云端硬盘-java.io.FileNotFoundException:document.txt(无此类文件或目录)

[英]Google drive - java.io.FileNotFoundException: document.txt (No such file or directory)

我正在使用Google Drive API示例中的代码在Drive中插入文件,该文件失败,出现java.io.FileNotFoundException:document.txt(无此类文件或目录)。 我已经注释掉了在云端硬盘中创建文件夹的代码,该代码没有任何问题。 这样我就可以通过身份验证了。 我要去哪里错了。

亲切的问候,伊恩。

public void saveToDrive(ServletContext sc){
        GoogleCredential googleCredential = getGoogleApiCredential(sc);
        Drive service = getDriveService(googleCredential);
        String parentId = null;
        try {
            About about = service.about().get().execute();
            System.out.println("Current user name: " + about.getName());

            System.out.println("Root folder ID:" + about.getRootFolderId());
            parentId = about.getRootFolderId();
            System.out.println("Total quota (bytes): " + about.getQuotaBytesTotal());
            System.out.println("Used quota (bytes): " + about.getQuotaBytesUsed());
    }catch (IOException e){

    }

        File body = new File();
        body.setTitle("Doc title");
        body.setDescription("A toast document");
        body.setMimeType("application/vnd.google-apps.file");
        body.setParents(Arrays.asList(new     ParentReference().setId(parentId)));


        java.io.File fileContent = new java.io.File("document.txt");
        FileContent mediaContent = new FileContent("plain/text", fileContent);
            //File body = new File();
            //body.setTitle("title");
            //body.setMimeType("application/vnd.google-apps.folder");


        try {
            //File file = service.files().insert(body).execute();
            File file = service.files().insert(body, mediaContent).execute();

            logger.severe("File id: " + file.getId());
        } catch (IOException e) {

        logger.severe(e.toString());
        }


    }

我没有时间完全重新运行您的方案,但是您可以使用我使用的方法对其进行测试(因此已经过测试)。 唯一明显的区别是,我在“正文”和“内容”中都粘贴了“相同的MIME类型”。 你不一样 我也不知道您的“ about.getRootFolderId()”会产生什么。 您可以尝试将“ root”字符串粘贴在那里以进行测试。

/**********************************************************************
   * create file/folder in GOODrive
   * @param prnId  parent's ID, (null or "root") for root
   * @param titl  file name
   * @param mime  file mime type (optional)
   * @param file  file (with content) to create
   * @return      file id  / null on fail
   */
  static String create(String prnId, String titl, String mime, java.io.File file) {
    String rsid = null;
    if (mGOOSvc != null && titl != null && file != null) {
      File meta = new File();
      meta.setParents(Arrays.asList(new ParentReference().setId(prnId == null ? "root" : prnId)));
      meta.setTitle(titl);
      if (mime != null)
        meta.setMimeType(mime);

      File gFl = mGOOSvc.files().insert(meta, new FileContent(mime, file)).execute();
      if (gFl != null && gFl.getId() != null) 
        rsid = gFl.getId();
    }
    return rsid;
  }

它来自此处工作的CRUD演示
祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM