[英]saving a urlconnection file to google cloud storage from app engine java
I am downloading a file (mp3) from a url and want to save it to google cloud storage via my appengine java app. 我正在从网址下载文件(mp3),并希望通过我的appengine Java应用程序将其保存到Google云存储中。 I can download the file and it plays fine, but when I save it to GCS and then play it it sounds funny and has a different file size. 我可以下载该文件,并且可以正常播放,但是当我将其保存到GCS并播放时,听起来很有趣,并且文件大小不同。 The code I am using is: 我使用的代码是:
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();
I tried to add .contentEncoding(connection.getContentEncoding()) to the FileOptionsBuilder, but connection.getContentEncoding() was returning null so that didn't work. 我试图将.contentEncoding(connection.getContentEncoding())添加到FileOptionsBuilder,但是connection.getContentEncoding()返回的是null,因此无法正常工作。
Is there something that I am doing wrong, maybe with setting the encoding? 我是否做错了,也许是设置了编码?
If it matters, the file created via this code can be seen here: http://commondatastorage.googleapis.com/ivrfiles%2F20001Address.mp3 如果有关系,可以在此处查看通过此代码创建的文件: http : //commondatastorage.googleapis.com/ivrfiles%2F20001Address.mp3
The working file downloaded through chrome is here: http://commondatastorage.googleapis.com/ivrfiles%2Fgen%20%282%29.mp3 通过chrome下载的工作文件位于: http : //commondatastorage.googleapis.com/ivrfiles%2Fgen%20%282%29.mp3
I find the bad file has the same five bytes randomly inserted in every now and then, but I don't know why... 我发现错误的文件有时会随机插入相同的五个字节,但是我不知道为什么...
You should not be using new ObjectOutputStream
. 您不应该使用new ObjectOutputStream
。 Remove that from your code and just write to the output stream you obtain from Channels.newOutputStream
directly. 从代码中删除它,然后直接写入从Channels.newOutputStream
获得的输出流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.