commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Alessandro.Koh...@swisscom.com>
Subject FileUpload Multipart Stream as input to PostMethod
Date Fri, 16 Feb 2007 11:12:18 GMT

hi there

(i've posted something similar in the HttpClient list, but maybe the
problem lies with FileUpload ..)

i'm using fileupload 2.0rc3 to get the parts of a multipart http request
as streams, pass them to the constructor of InputStreamRequestEntity
(HttpClient 3.0.1) and then executing the PostMethod.

here's the code:

        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iter = upload.getItemIterator(clientRequest);
        while (iter.hasNext()) {
          FileItemStream item = iter.next();
          String name = item.getFieldName();
          if (item.isFormField()) {
            postMethod.addParameter(name,
Streams.asString(item.openStream()));
          } else {
            postMethod.setRequestEntity(new
InputStreamRequestEntity(item.openStream()));
          }
        }      

i have verified by debugging that a 'file part' is found, it's name can
be read correctly .. but upon executing the post method, the following
exception is thrown:

java.lang.IllegalStateException: Content must be set before entity is
written
        at
org.apache.commons.httpclient.methods.InputStreamRequestEntity.writeRequ
est(InputStreamRequestEntity.java:176)
        at
org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequest
Body(EntityEnclosingMethod.java:495)
        at
org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase
.java:1973)
        at
org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java
:993)
        at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMe
thodDirector.java:397)
        at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMetho
dDirector.java:170)
        at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:3
96)
        at
org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:3
24)

is this line of code supposed to work at all?

        postMethod.setRequestEntity(new
InputStreamRequestEntity(item.openStream()));

writeRequestBody in the EntityEnclosingMethod gets a content length of
0, although i can verify that the content in the original incoming http
request had a length of 280678.

thanks for any help!

cheers
alessandro

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message