Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a7b35b

Browse files
committed
Enable connection keep alive if there is more than one file to upload
1 parent 6c517d2 commit 9a7b35b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/com/alexbbb/uploadservice/UploadService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private void handleFileUpload(final String uploadId, final String url, final Str
159159
InputStream responseStream = null;
160160

161161
try {
162-
conn = getMultipartHttpURLConnection(url, method, boundary);
162+
conn = getMultipartHttpURLConnection(url, method, boundary, filesToUpload.size());
163163

164164
setRequestHeaders(conn, requestHeaders);
165165

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

236-
private
237-
HttpURLConnection
238-
getMultipartHttpURLConnection(final String url, final String method, final String boundary)
239-
throws IOException {
236+
private HttpURLConnection getMultipartHttpURLConnection(final String url, final String method,
237+
final String boundary, int totalFiles) throws IOException {
240238
final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
241239

242240
conn.setDoInput(true);
243241
conn.setDoOutput(true);
244242
conn.setUseCaches(false);
245243
conn.setChunkedStreamingMode(0);
246244
conn.setRequestMethod(method);
247-
conn.setRequestProperty("Connection", "close");
245+
if (totalFiles <= 1) {
246+
conn.setRequestProperty("Connection", "close");
247+
} else {
248+
conn.setRequestProperty("Connection", "Keep-Alive");
249+
}
248250
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
249251
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
250252

0 commit comments

Comments
 (0)