1

I am downloading a file (mp3) from a url and want to save it to google cloud storage via my appengine java app. 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. 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.

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

The working file downloaded through chrome is here: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...

1 Answer 1

2

You should not be using new ObjectOutputStream. Remove that from your code and just write to the output stream you obtain from Channels.newOutputStream directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.