Skip to content

Commit

Permalink
Enable connection keep alive if there is more than one file to upload
Browse files Browse the repository at this point in the history
  • Loading branch information
gotev committed Jan 28, 2015
1 parent 6c517d2 commit 9a7b35b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/com/alexbbb/uploadservice/UploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void handleFileUpload(final String uploadId, final String url, final Str
InputStream responseStream = null;

try {
conn = getMultipartHttpURLConnection(url, method, boundary);
conn = getMultipartHttpURLConnection(url, method, boundary, filesToUpload.size());

setRequestHeaders(conn, requestHeaders);

Expand Down Expand Up @@ -233,18 +233,20 @@ private byte[] getTrailerBytes(final String boundary) throws UnsupportedEncoding
return builder.toString().getBytes("US-ASCII");
}

private
HttpURLConnection
getMultipartHttpURLConnection(final String url, final String method, final String boundary)
throws IOException {
private HttpURLConnection getMultipartHttpURLConnection(final String url, final String method,
final String boundary, int totalFiles) throws IOException {
final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setChunkedStreamingMode(0);
conn.setRequestMethod(method);
conn.setRequestProperty("Connection", "close");
if (totalFiles <= 1) {
conn.setRequestProperty("Connection", "close");
} else {
conn.setRequestProperty("Connection", "Keep-Alive");
}
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

Expand Down

0 comments on commit 9a7b35b

Please sign in to comment.