【发布时间】:2014-08-21 02:06:50
【问题描述】:
我在使用存储桶的谷歌服务将文件下载到存储桶时遇到了一些问题。我花了一些时间在互联网上搜索并没有答案。
代码如下:
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;
【问题讨论】: