【问题标题】:Unable to upload file in specific folder in google drive using REST API无法使用 REST API 在谷歌驱动器的特定文件夹中上传文件
【发布时间】:2022-07-07 16:47:00
【问题描述】:

我正在尝试使用来自 android 应用程序的 REST API 将本地文件上传到 Google Drive 中的指定文件夹,该文件正在根目录中上传,但未在指定文件夹中上传。 API 中没有错误。

使用以下 API: https://developers.google.com/drive/api/guides/manage-uploads#multipart

尝试通过以下方式在元数据中传递文件夹 ID:

1.

String[] arr = {parentFolderId};    
jsonObject.put("parents", arr);
jsonObject.put("name", file.getName());
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", parentFolderId);
jsonArray.put(jsonObject1);
jsonObject.put("parents", jsonArray);
jsonObject.put("name", file.getName());
ArrayList<String> arrayList = new ArrayList<>(); 
arrayList.add(parentFolderId);
jsonObject.put("parents", arrayList);
jsonObject.put("name", file.getName());

“parents”参数不起作用,也尝试使用“addParents”参数。 请提出一种指定父文件夹 ID 的方法。

【问题讨论】:

  • 以后你应该包含你从这些调用中得到的错误信息。

标签: java android google-api google-drive-api google-api-java-client


【解决方案1】:

我不是 java 或 android 专家,但这可能会让您入门。

String folderId = "folderID";
File fileMetadata = new File();
fileMetadata.setName("FileName");
fileMetadata.setParents(Collections.singletonList(folderId));

File file = driveService.files().create(fileMetadata)
.setFields("id, parent")
.execute();
System.out.println("File ID: " + file.getId());

或者这个

String UploadFileToGoogleDrive(String sFullPath, String sFileName, String sParentFolderId) {               
    com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
    fileMetadata.setName(sFileName);
    fileMetadata.setParents(Collections.singletonList(sParentFolderId));
    java.io.File filePath = new java.io.File(sFullPath);
    FileContent mediaContent = new FileContent(null, filePath);
    try {
        com.google.api.services.drive.model.File file = googleDriveService.files().create(fileMetadata, mediaContent)
                .setFields("id, parents")
                .execute();
        return file.getId();
    } catch (IOException e) {            
        return null;
    }       
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多