You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Dennis SELLINGER <de...@vigis.com> on 2003/08/14 10:47:17 UTC

(HttpClient) Posting large files using MultipartPostMethod

Hi,

I have been trying to implement a simple client to upload a file to a servlet.  The code I use is pretty basic (summarized below).  I find that it works for files that are sufficiently small (<128 K) but gives the following error for larger files:

14 août 2003 10:19:55 com.vigis.designfilereplication.Replicator postReplicationRequest
GRAVE: I/O error while executing replication post request... root cause Software caused connection abort: socket write error
java.net.SocketException: Software caused connection abort: socket write error
	at java.net.SocketOutputStream.socketWrite0(Native Method)
	at java.net.SocketOutputStream.socketWrite(Unknown Source)
	at java.net.SocketOutputStream.write(Unknown Source)
	at org.apache.commons.httpclient.HttpConnection$WrappedOutputStream.write(HttpConnection.java:1344)
	at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
	at java.io.BufferedOutputStream.write(Unknown Source)
	at org.apache.commons.httpclient.methods.multipart.FilePart.sendData(FilePart.java:300)
	at org.apache.commons.httpclient.methods.multipart.Part.send(Part.java:294)
	at org.apache.commons.httpclient.methods.multipart.Part.sendParts(Part.java:344)
	at org.apache.commons.httpclient.methods.MultipartPostMethod.writeRequestBody(MultipartPostMethod.java:262)
	at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2224)
	at org.apache.commons.httpclient.HttpMethodBase.processRequest(HttpMethodBase.java:2534)
	at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1047)
	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:638)
	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:500)
	at com.vigis.designfilereplication.Replicator.postReplicationRequest(Replicator.java:424)
	at com.vigis.designfilereplication.Replicator.main(Replicator.java:170)

My client is running on Java 1.4.2 under windows xp sp1.  In the case where the post fails, I don't think that it arrives at my server (i.e. my servlet does not start running the getPost() method).  My server is a servlet running under tomcat 4.1.x using the com.oreilly.servlet.MultipartRequest API to handle the upload.  This servlet has be used extensively with multipart posts originalating from Internet Explorer, so there should be no problems with it.

Here is a cleaned up version of the code I am executing:

		MultipartPostMethod post = new MultipartPostMethod("http://alsace/vwjserv/accept/dea93");
		HttpClient client = new HttpClient();
		client.setConnectionTimeout(5000);
		UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("-----","-----");
		client.getState().setCredentials("realm","host",credentials);
		post.setDoAuthentication(true);
		try {
			File archive1 = new File("c:/test.txt");
			post.addParameter("VIGIS_ULD00",archive1);
		} catch (FileNotFoundException e1) {
			...
		}
		post.addParameter("instant",Long.toHexString(this.instant.getTime()));
		int statusCode = -1;
		int retryCount = 0;
		boolean retry = true;
		while (retry) {
			try {
				statusCode = client.executeMethod(post);
				retry = false;
			} catch (HttpRecoverableException hrx) {
				...
			} catch (HttpException e) {
				...
			} catch (IOException e) {			
				...
			}
		}

I think the code is pretty clean, but I am obviously missing a parmeter or something. Any help would be appreciated.

Thanks in advance,
Dennis.

Dennis Sellinger
Geotech
France.

Re: (HttpClient) Posting large files using MultipartPostMethod

Posted by an...@gmx.net.
Hi Dennis,

you might  try to set the contentLength in PostMethod to the right length or
to CONTENT_LENGTH_CHUNKED = -1.

Regards,
Andreas




> Hi,
> 
> I have been trying to implement a simple client to upload a file to a 
> servlet.  The code I use is pretty basic (summarized below).  I find 
> that it works for files that are sufficiently small (<128 K) but gives 
> the following error for larger files:
> 
> 14 août 2003 10:19:55 com.vigis.designfilereplication.Replicator 
> postReplicationRequest
> GRAVE: I/O error while executing replication post request... root cause 
> Software caused connection abort: socket write error
> java.net.SocketException: Software caused connection abort: socket write 
> error
> 	at java.net.SocketOutputStream.socketWrite0(Native Method)
> 	at java.net.SocketOutputStream.socketWrite(Unknown Source)
> 	at java.net.SocketOutputStream.write(Unknown Source)
> 	at 
> org.apache.commons.httpclient.HttpConnection$WrappedOutputStream.write(Ht
> tpConnection.java:1344)
> 	at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
> 	at java.io.BufferedOutputStream.write(Unknown Source)
> 	at 
> org.apache.commons.httpclient.methods.multipart.FilePart.sendData(FilePar
> t.java:300)
> 	at 
> org.apache.commons.httpclient.methods.multipart.Part.send(Part.java:294)
> 	at 
> org.apache.commons.httpclient.methods.multipart.Part.sendParts(Part.java:
> 344)
> 	at 
> org.apache.commons.httpclient.methods.MultipartPostMethod.writeRequestBod
> y(MultipartPostMethod.java:262)
> 	at 
> org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.
> java:2224)
> 	at 
> org.apache.commons.httpclient.HttpMethodBase.processRequest(HttpMethodBas
> e.java:2534)
> 	at 
> org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:
> 1047)
> 	at 
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:63
> 8)
> 	at 
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:50
> 0)
> 	at 
> com.vigis.designfilereplication.Replicator.postReplicationRequest(Replica
> tor.java:424)
> 	at com.vigis.designfilereplication.Replicator.main(Replicator.java:170)
> 
> My client is running on Java 1.4.2 under windows xp sp1.  In the case 
> where the post fails, I don't think that it arrives at my server (i.e. 
> my servlet does not start running the getPost() method).  My server is a 
> servlet running under tomcat 4.1.x using the 
> com.oreilly.servlet.MultipartRequest API to handle the upload.  This 
> servlet has be used extensively with multipart posts originalating from 
> Internet Explorer, so there should be no problems with it.
> 
> Here is a cleaned up version of the code I am executing:
> 
> 		MultipartPostMethod post = new 
> MultipartPostMethod("http://alsace/vwjserv/accept/dea93");
> 		HttpClient client = new HttpClient();
> 		client.setConnectionTimeout(5000);
> 		UsernamePasswordCredentials credentials = new 
> UsernamePasswordCredentials("-----","-----");
> 		client.getState().setCredentials("realm","host",credentials);
> 		post.setDoAuthentication(true);
> 		try {
> 			File archive1 = new File("c:/test.txt");
> 			post.addParameter("VIGIS_ULD00",archive1);
> 		} catch (FileNotFoundException e1) {
> 			...
> 		}
> 		post.addParameter("instant",Long.toHexString(this.instant.getTime()));
> 		int statusCode = -1;
> 		int retryCount = 0;
> 		boolean retry = true;
> 		while (retry) {
> 			try {
> 				statusCode = client.executeMethod(post);
> 				retry = false;
> 			} catch (HttpRecoverableException hrx) {
> 				...
> 			} catch (HttpException e) {
> 				...
> 			} catch (IOException e) {			
> 				...
> 			}
> 		}
> 
> I think the code is pretty clean, but I am obviously missing a parmeter 
> or something. Any help would be appreciated.
> 
> Thanks in advance,
> Dennis.
> 
> Dennis Sellinger
> Geotech
> France.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 

-- 
COMPUTERBILD 15/03: Premium-e-mail-Dienste im Test
--------------------------------------------------
1. GMX TopMail - Platz 1 und Testsieger!
2. GMX ProMail - Platz 2 und Preis-Qualitätssieger!
3. Arcor - 4. web.de - 5. T-Online - 6. freenet.de - 7. daybyday - 8. e-Post


Re: (HttpClient) Posting large files using MultipartPostMethod

Posted by Michael Becke <be...@u.washington.edu>.
Hi Dennis,

In general you code looks fine.  My only question is how many time you 
are running though the try/catch loop.

This error leads me to believe that the server is closing the socket 
after some period of time.  Either it does not like something about the 
post or it cannot handle the size of request you are sending.  There are 
a few things I would suggest:

  - analyze the server logs to see if there is any indication why the 
post is being rejected.
  - try using a different server side component to accept the file 
upload.  I would suggest commons FileUpload 
<http://jakarta.apache.org/commons/fileupload/index.html>.  This will 
help to narrow down the problem as is has been successfully used in the 
past.  Most browsers do not conform to the multipart post 
specification(RFC 1867) very well and as such are not always a good 
indicator of how HttpClient will fare.
  - Take a look at the wire log 
<http://jakarta.apache.org/commons/httpclient/logging.html>.  This may 
offer some useful details, but be careful as it will contain all of the 
data being posted(headers and content).
  - Fiddle with the Expect header.  Try the upload with 
MultipartPostMethod.setUseExpectHeader() set to both true and false.

I hope this helps.

Mike

Dennis SELLINGER wrote:
> Hi,
> 
> I have been trying to implement a simple client to upload a file to a servlet.  The code I use is pretty basic (summarized below).  I find that it works for files that are sufficiently small (<128 K) but gives the following error for larger files:
> 
> 14 août 2003 10:19:55 com.vigis.designfilereplication.Replicator postReplicationRequest
> GRAVE: I/O error while executing replication post request... root cause Software caused connection abort: socket write error
> java.net.SocketException: Software caused connection abort: socket write error
> 	at java.net.SocketOutputStream.socketWrite0(Native Method)
> 	at java.net.SocketOutputStream.socketWrite(Unknown Source)
> 	at java.net.SocketOutputStream.write(Unknown Source)
> 	at org.apache.commons.httpclient.HttpConnection$WrappedOutputStream.write(HttpConnection.java:1344)
> 	at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
> 	at java.io.BufferedOutputStream.write(Unknown Source)
> 	at org.apache.commons.httpclient.methods.multipart.FilePart.sendData(FilePart.java:300)
> 	at org.apache.commons.httpclient.methods.multipart.Part.send(Part.java:294)
> 	at org.apache.commons.httpclient.methods.multipart.Part.sendParts(Part.java:344)
> 	at org.apache.commons.httpclient.methods.MultipartPostMethod.writeRequestBody(MultipartPostMethod.java:262)
> 	at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2224)
> 	at org.apache.commons.httpclient.HttpMethodBase.processRequest(HttpMethodBase.java:2534)
> 	at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1047)
> 	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:638)
> 	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:500)
> 	at com.vigis.designfilereplication.Replicator.postReplicationRequest(Replicator.java:424)
> 	at com.vigis.designfilereplication.Replicator.main(Replicator.java:170)
> 
> My client is running on Java 1.4.2 under windows xp sp1.  In the case where the post fails, I don't think that it arrives at my server (i.e. my servlet does not start running the getPost() method).  My server is a servlet running under tomcat 4.1.x using the com.oreilly.servlet.MultipartRequest API to handle the upload.  This servlet has be used extensively with multipart posts originalating from Internet Explorer, so there should be no problems with it.
> 
> Here is a cleaned up version of the code I am executing:
> 
> 		MultipartPostMethod post = new MultipartPostMethod("http://alsace/vwjserv/accept/dea93");
> 		HttpClient client = new HttpClient();
> 		client.setConnectionTimeout(5000);
> 		UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("-----","-----");
> 		client.getState().setCredentials("realm","host",credentials);
> 		post.setDoAuthentication(true);
> 		try {
> 			File archive1 = new File("c:/test.txt");
> 			post.addParameter("VIGIS_ULD00",archive1);
> 		} catch (FileNotFoundException e1) {
> 			...
> 		}
> 		post.addParameter("instant",Long.toHexString(this.instant.getTime()));
> 		int statusCode = -1;
> 		int retryCount = 0;
> 		boolean retry = true;
> 		while (retry) {
> 			try {
> 				statusCode = client.executeMethod(post);
> 				retry = false;
> 			} catch (HttpRecoverableException hrx) {
> 				...
> 			} catch (HttpException e) {
> 				...
> 			} catch (IOException e) {			
> 				...
> 			}
> 		}
> 
> I think the code is pretty clean, but I am obviously missing a parmeter or something. Any help would be appreciated.
> 
> Thanks in advance,
> Dennis.
> 
> Dennis Sellinger
> Geotech
> France.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>