opera
opera

Reputation: 13

Stream multiple files over HttpUrlConnection

My application need to transfer multiple files to an http server (by opening OutputStream from HttpUrlConnection) but to avoid overhead of connection establishment we would like to use one connection only. Would this be feasible?

Note: The data is created in real time so that we cannot add them into one archive file and transfer with one shot.

Thanks for your advices!

Upvotes: 0

Views: 837

Answers (3)

user207421
user207421

Reputation: 310957

You're over-optimizing. HttpURLConnection already does TCP connection pooling behind the scenes. Just use a new URL, HttpURLConnection, OutputStream, etc., per file.

Upvotes: 3

fge
fge

Reputation: 121740

The fact that you have to output more than one file does not prevent the fact that you can still use an archive format which can be created using your OutputStream, in real time; and zip is such a format.

The JDK has ZipOutputStream which can help you there; basically you can use it as such (code to set the HTTP headers not shown):

// out is your HttpUrlConnection's OutputStream

try (
    final ZipOutputStream zout = new ZipOutputStream(out);
) {
    addEntries(zout);
}

The addEntries() method would then create ZipEntry instances, one per file, and write the contents.

Upvotes: 0

DiogoSantana
DiogoSantana

Reputation: 2434

Try to use Apache HttpClient. It supports HTTP 1.1 keep-alive feature.

Reference: http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html

Fast read: http://en.wikipedia.org/wiki/HTTP_persistent_connection

Upvotes: -2

Related Questions