我在使用Google存储桶服务将文件下载到存储桶时遇到一些问题。我花了一些时间在互联网上搜索,但没有答案。
这是代码:
public static void downloadFileFromBucket(String stringUrl, String pathParaDownload) throws IOException {
URL somefile = new URL(stringUrl);
ReadableByteChannel rbc = null;
try {
rbc = Channels.newChannel(somefile.openStream());
} catch (IOException e) {
e.printStackTrace();
return;
}
FileOutputStream fos = new FileOutputStream(pathParaDownload);
long start = System.currentTimeMillis();
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
long end = System.currentTimeMillis();
System.out.println("Tempo de download: ");
System.out.println(end-start);
fos.close();
}
这是控制台日志:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://storage.googleapis.com/bucket-bruno/?key=AIzaSyBTOqMtlFD7KUsK7p9ObGlMBOJuQRyMxuM
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
at java.net.URL.openStream(URL.java:1037)
at projetoIntegracao.projetoIntegracao.DownloadBucket.downloadFileFromBucket(DownloadBucket.java:41)
at projetoIntegracao.projetoIntegracao.DownloadBucket.main(DownloadBucket.java:33)
编辑:这是变量的构造:
String stringUrl = "http://storage.googleapis.com/mybucketname/"+fileNameWithExtension+"?key="+apiKey;
由于HTTP 400是无效的sintax,因此您需要验证参数“ stringUrl”,因为问题就在那儿。
如果您可以发布生成该变量的代码,则将很有帮助。
如果您有权限下载文件或使用OAuth,请查看以下内容:https://developers.google.com/console/help/new/#generatingoauth2
编辑:好的,看来您没有使用OAuth进行下载身份验证,因此您需要让文件具有打开权限,看看示例:
但我建议使用OAuth 2.0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句