![](/img/trans.png)
[英]Upload file to Google cloud storage from Google App Engine (Java)
[英]saving a urlconnection file to google cloud storage from app engine java
我正在从网址下载文件(mp3),并希望通过我的appengine Java应用程序将其保存到Google云存储中。 我可以下载该文件,并且可以正常播放,但是当我将其保存到GCS并播放时,听起来很有趣,并且文件大小不同。 我使用的代码是:
URL url = new URL(link);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(20000);
connection.setReadTimeout(20000);
InputStream input = connection.getInputStream();
GcsFileOptions options = new GcsFileOptions.Builder().acl("public-read").build();
GcsOutputChannel fileChannel = gcsService.createOrReplace(new GcsFilename("ivrfiles", propertyId.toString()+"Address.mp3"), options);
ObjectOutputStream oout = new ObjectOutputStream(Channels.newOutputStream(fileChannel));
byte[] buffer = new byte[4096];
int n = - 1;
while ( (n = input.read(buffer)) != -1)
{
oout.write(buffer, 0, n);
}
oout.close();
我试图将.contentEncoding(connection.getContentEncoding())添加到FileOptionsBuilder,但是connection.getContentEncoding()返回的是null,因此无法正常工作。
我是否做错了,也许是设置了编码?
如果有关系,可以在此处查看通过此代码创建的文件: http : //commondatastorage.googleapis.com/ivrfiles%2F20001Address.mp3
通过chrome下载的工作文件位于: http : //commondatastorage.googleapis.com/ivrfiles%2Fgen%20%282%29.mp3
我发现错误的文件有时会随机插入相同的五个字节,但是我不知道为什么...
您不应该使用new ObjectOutputStream
。 从代码中删除它,然后直接写入从Channels.newOutputStream
获得的输出流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.