我正在使用Java来创建应用程序。然而,我无法解决从驱动器下载文件的问题。
我正在尝试使用Google开发人员页面上给出的函数。(链接如下)
https://developers.google.com/drive/manage-downloads
但是,对于如何获取/生成特定文件的downloadURI,并没有明确的说明。另外,我也不知道如何使用downloadURI下载文件。
我正在使用以下函数-
private static InputStream downloadFile(Drive service, File file)
{
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0)
{
try
{
HttpResponse resp = service.getRequestFactory().buildGetRequest
(new GenericUrl(file.getDownloadUrl())).execute();
return resp.getContent();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
else
{
return null;
}
}
在这里,我不能获得哪个文件应该是函数的输入参数。请帮帮我。
发布于 2014-01-31 10:36:48
这就是我是如何做到的。我下载的不是文档,而是给定文档的所有修订版。所以逻辑是一样的。
private void downloadRevisions(final RevisionList revisions,
final Drive service) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
//TODO for debugging
int i = 0;
for (Revision revision : revisions.getItems()) {
System.out.println("Thread running: " + i);
if (revision.getExportLinks() != null
&& revision.getExportLinks().get("text/plain") != null
&& revision.getExportLinks().get("text/plain").length() > 0) {
HttpResponse resp = service
.getRequestFactory()
.buildGetRequest(
new GenericUrl(revision
.getExportLinks().get("text/plain")))
.execute();
//InputStream inputStream = resp.getContent();
//File Name
String revisionFileName = docId+"_"+revision.getId()+"_"+revision.getModifiedDate();
FileOutputStream outputstream = new FileOutputStream(new java.io.File(revisionFileDir, revisionFileName));
IOUtils.copy(resp.getContent(), outputstream,true);
outputstream.close();
// writeToFile(inputStream);
System.out.println("downloading: " + revisionFileName);
}
//TODO for debugging
i++;
}
System.out.println("Downloading Done. Document Id: "+docId);
} catch (IOException e) {
Log.info("WriteToFile Fail", e.toString());
e.printStackTrace();
}finally{
//if( doneFW !=null)
// doneFW.close();
}
}
});
thread.start();
}
发布于 2015-09-30 10:34:55
在调用方法中,设置fileId和url位置
例如:
com.google.api.services.drive.model.File myFile = new com.google.api.services.drive.model.File();
myFile.setDownloadUrl("https://drive.google.com/drive/u/0/folders/<encrypted_id>");
myFile.setId("<encrypted_id>");
Drive service = getDriveService(); // Assuming you have a method to authorize and get drive
BufferedReader br = new BufferedReader(new InputStreamReader(downloadFile(service,myFile));
....
....
...
注意:要获取url,请导航到该文件,右键单击该文件,然后选择"Get link“。
发布于 2013-11-27 14:32:14
看看这个
https://developers.google.com/drive/v2/reference/files/list
这将返回来自驱动器的文件列表,然后你只需要从这个列表中传递一个你想要下载的文件
https://stackoverflow.com/questions/20235104
复制相似问题