(Java)下载网址无效

编程入门 行业动态 更新时间:2024-10-28 18:36:11
本文介绍了(Java)下载网址无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力尝试使用谷歌驱动器API下载文件。我只是写代码,应该从我的驱动器下载文件到我的电脑上。我终于进入了一个认证阶段,可以查看文件元数据。出于某种原因,我仍然无法下载文件。我收到的downLoadURL如下所示:

I am battling with trying to download files using the google drive API. I'm just writing code that should download files from my drive onto my computer. I've finally got to a stage where I am authenticated and can view the file metadata. For some reason, I'm still unable to download files. The downLoadURL I get looks like:

doc-04-as-docs.googleusercontent/docs/securesc/XXXXXXXXXXXXXX/0B4dSSlLzQCbOXzAxNGxuRUhVNEE?e=download&gd=true

当我运行我的代码或将其复制并粘贴到浏览器中时,URl不会下载任何内容。但是,在浏览器中,当我删除URL的& gd = true部分时,它会下载该文件。

This URl isn't downloading anything when I run my code or when I copy and paste it in a browser. But, in the browser, when i remove the "&gd=true" part of the URL it downloads the file.

我的下载方法是直接从Google驱动器API文档:

My download method is straight out of the google drive API documentation:

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; } }

任何人都知道这是怎么回事?

Anyone know whats going on here?

在此先感谢。

推荐答案

由于您使用Drive v2,方法(同样在文档上)是为了获得 InputStream 通过 HttpRequest 对象。

Since you're using Drive v2, a different approach (also on the documentation) is for you to get the InputStream thru the HttpRequest object.

/** * 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; } }

更多推荐

(Java)下载网址无效

本文发布于:2023-10-29 12:50:11,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网址   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!