简体   繁体   English

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

[英]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.

相关问题 通过Google App Engine(Java)将文件上传到Google云端存储 - Upload file to Google cloud storage from Google App Engine (Java) 适用于Java和Google云存储的Google App Engine - Google App Engine for Java and Google Cloud Storage 通过Google App Engine(JAVA)将文件(图像或视频)从JSP上传到Google云存储 - Upload File (image or video) from JSP to google cloud storage via Google app engine(JAVA) 使用blobstore api从云存储中检索文件的Google App引擎Java错误 - google app engine java error using blobstore api to retrieve file from cloud storage 通过Google App Engine Servlet上传ZIP,并将内容保存到Cloud Storage(Java) - Uploading ZIP via Google App Engine servlet, and saving content to Cloud Storage (Java) 使用 Java 通过 Google App Engine 将文件上传到 Google Cloud Storage:(没有这样的文件或目录) - Uploading Files to Google Cloud Storage via Google App Engine using Java: (No such file or directory) Google App Engine中的Google云存储文件位置 - Google cloud storage file location in google app engine 无法通过Google App Engine Servlet(Java)将对象存储在Google Cloud Storage中 - Cannot store objects in my Google Cloud Storage from my Google App Engine Servlet (Java) Google App Engine:从Google Cloud Storage中读取 - Google App Engine : Read from Google Cloud Storage 如何使用Java App Engine正确上传(映像)文件到Google云端存储? - How to properly upload (image) file to Google Cloud Storage using Java App Engine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM