你需要为文件上传到特定的文件夹,做的是文件夹的ID添加到该文件的父母(参考文档的
file.insert)。
因此,您需要做的全部事情是:
- 在驱动器中找到文件夹
- 创建插入请求
- 将父ID添加到文件插入中
- 执行插入请求
码
查找文件夹
在这里,您可以选择2种方式:循环浏览所有文件夹或进行搜索。
更快,更省力的是按名称搜索文件夹;这样做很简单:
//Search by name and type folderString qStr = "mimeType = 'application/vnd.google-apps.folder' and title = 'myFolder'";//Get the list of FoldersFileList fList=service.files().list().setQ(qStr).execute();//Check that the result is one folderFile folder;if (fList.getItems().lenght==0){ folder=fList.getItems()[0];}有关可能的搜索参数的更多信息。
创建插入请求,如示例中所示
File file = service.files().insert(body, mediaContent);
在执行之前,我们需要设置父文件。setParents(Arrays.asList(new
ParentReference()。setId(folder.getFolderId())));
最后执行请求文件。
我尚未测试代码,因此可能在这里和那里有一些错字。



