acctman
acctman

Reputation: 4349

Java urlConnection, saving audio file to server?

I've seen examples with text files but is saving an audio file directly to a server done the same way with URLConnection?

Upvotes: 2

Views: 1078

Answers (1)

Joel
Joel

Reputation: 30146

Yes, the same. Although make sure you use a binary output stream to write the content to disk.

Something like:

    URLConnection conn = new URL("http://www.gravatar.com/avatar/fd9e8761fad999a1bf1e095fc8f53ffe?s=32&d=identicon&r=PG")
            .openConnection();
    InputStream is = conn.getInputStream();
    FileOutputStream outstream = new FileOutputStream("/tmp/myfile");
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
        outstream.write(buffer, 0, len);
    }
    outstream.close();
    is.close();

The example uses your gravatar, but same difference.

Upvotes: 2

Related Questions