我正在从 URL 下载文件 (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
获取的输出流。
关于java - 将 urlconnection 文件从 App Engine java 保存到 Google 云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20385523/