(Java)下载URL不起作用
我正在努力尝试使用谷歌驱动器API下载文件。 我只是编写应该从我的驱动器下载文件到我的计算机上的代码。 我终于到了一个我经过身份validation的阶段,可以查看文件元数据。 出于某种原因,我仍然无法下载文件。 我得到的downLoadURL看起来像:
https://doc-04-as-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXX/0B4dSSlLzQCbOXzAxNGxuRUhVNEE?e=download&gd=true
当我运行代码或将其复制并粘贴到浏览器中时,此URl不会下载任何内容。 但是,在浏览器中,当我删除URL的“&gd = true”部分时,它会下载该文件。
我的下载方法直接来自google drive API文档:
public static InputStream downloadFile(Drive service, File file) { if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) { try { System.out.println("Downloading: "+ file.getTitle()); return service.files().get(file.getId()).executeMediaAsInputStream(); } catch (IOException e) { // An error occurred. e.printStackTrace(); return null; } } else { // The file doesn't have any content stored on Drive. return null; } }
有谁知道这里发生了什么?
提前致谢。
由于您使用的是Drive v2,因此您可以通过HttpRequest
对象获取InputStream
的不同方法(也在文档中 )。
/** * Download a file's content. * * @param service Drive API service instance. * @param file Drive File instance. * @return InputStream containing the file's content if successful, * {@code null} otherwise. */ 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) { // An error occurred. e.printStackTrace(); return null; } } else { // The file doesn't have any content stored on Drive. return null; } }
- 如何在Android Studio中手动安装App Engine?
- 使用带自动缩放的java的GAE上的Cron作业失败
- java,驱动APIv2,appengine 1.7.3,struts2.3.4.1,OAuth2。 TokenResponseException:400 OK invalid_grant
- 关于GAE的REST,使用什么实现?(RESTlet,jersey,other ……)
- 写入CSV文件,然后在Appengine(Java)中将其压缩
- GAE,JPA,XG-transactions,太多实体组exception
- 如何在Servlets中插入JSPfunction?
- 如何避免GoogleAppEngine DataStore延迟?
- appspot xmpp与jabber.org交谈