java - 将 urlconnection 文件从 App Engine java 保存到 Google 云存储

标签 java google-app-engine google-cloud-storage

我正在从 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/

相关文章:

javascript - 管理可扩展的 java/javascript 应用程序

java - 如何对spring的网关进行单元测试?

firebase - 云函数 : zip multiple documents from Cloud Storage

amazon-s3 - 如何使用 Google Cloud Storage 隐藏真实 URL?

python - 如何使用 Python 以追加模式打开 GCS 存储桶中的文件?

java - 如何在 Android 7.0 的微调器模式下修复 DatePickerDialog?

java - 使用 jdbc 时出错

java - 将图像从 Android 客户端发送到 AppEngine Cloud Endpoint

python - Google App Engine、ReportLab 和 True Type 字体

python - 包含分号的 POST 在分号后被截断