Showing posts with label httpclient large files putmethod. Show all posts
Showing posts with label httpclient large files putmethod. Show all posts

Tuesday, July 21, 2009

HttpClient uploading large files

When using HTTPClient to upload large files I was getting the following exception:

org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.SocketException) caught when processing request: Software caused connection abort: socket write error
org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
java.lang.Exception: Unbuffered entity enclosing request can not be repeated.

I had the following code:

PutMethod method = new PutMethod(url + "/" + file.getName());
RequestEntity requestEntity = new InputStreamRequestEntity(new FileInputStream(file));
method.setRequestEntity(requestEntity);
client.executeMethod(method);

I replaced the code as follows:

String filename = url + "/" + file.getName();
PutMethod method = new PutMethod(filename);
method.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
Part[] parts = { new FilePart(file.getName(), file) };
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
client.executeMethod(method);

In my case I am uploading (PUTMETHOD) to a WebDAV folder. But I noticed that this issue is happening for other protocols too.
Basically using MultiPart and setting the parameter USE_EXPECT_CONTINUE to true solved the problem.
The problem is due to the authentication. When the post has a big file it opens the transfer multiple times. And I guess the socket will be closed or not authenticated when there is the second transfer.