Google drive api 在google drive api中复制文档内容

Google drive api 在google drive api中复制文档内容,google-drive-api,Google Drive Api,我需要使用google drive Api for Java将一个文档的内容复制到另一个文档中,这两个文档都存储在google drive中。我可以上传或下载文档,但我不知道如何将文档内容直接传输到另一个文档中。我想到了这样的事情: public void copyContent(String sourceId, String destinationId) { File sourceFile = service.files().get(sourceId).execute(); Abs

我需要使用google drive Api for Java将一个文档的内容复制到另一个文档中,这两个文档都存储在google drive中。我可以上传或下载文档,但我不知道如何将文档内容直接传输到另一个文档中。我想到了这样的事情:

public void copyContent(String sourceId, String destinationId) {
   File sourceFile = service.files().get(sourceId).execute();
   AbstractInputStreamContent content = null; //sourceFile.getContent()??? :-(

   File destinationFile = service.files().get(destinationId).execute();
   service.files().update(destinationId, destinationFile, content).execute();
}

有没有一种方法可以将文件内容获取为AbstractInputStreamContent?或者可能存在另一种复制内容的方法?我需要一个不导出/上载文档的解决方案,因为我注意到一些转换问题。谢谢

您可以使用驱动器API的
files.copy
方法创建现有文档的副本:


嗨,克劳迪奥,非常感谢你的回答。我知道copy()方法,但它将使用新id创建一个新文件。我希望保留旧id(“在我的示例中为destinationId”),因为我的应用程序在许多地方引用它。也许我可以设置由copy()创建的新文件的id并删除“目标文件”?您无法设置id,因此我猜您必须下载源文件并使用其内容更新目标文件。下载文档意味着我应该使用支持的格式(例如.odt)导出它。对吗?然后,当我上传文件时,它将被重新转换为“谷歌文档”。不幸的是,我已经有过这个过程的经验,我注意到将文件重新转换为“谷歌文档”(例如,彩色表格边框变为黑色)时存在一些问题。是否可以直接使用“google文档”格式下载文档:application/vnd.google apps.document?好的,我将使用google apps脚本编写一个copyContent()函数,将其部署为web应用程序,并从google Drive API调用它。谢谢你提供的有用信息!