You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by "Segal, Jeffrey" <Je...@solers.com> on 2006/10/26 23:56:53 UTC

multipart post problem

Hello all,
 
I've been having some problems trying to use HttpClient to upload files.
My platform:
 
Client: Stand-alone Java client, httpclient: 3.0.1
Server: Weblogic 9.1, commons-fileupload 1.1.1
 
When I execute the code I attached below (trimmed a bit for space),
which appears to be correct per the tutorial, I receive the following
error:
 
"Upload failed: org.apache.commons.fileupload.FileUploadException: the
request was rejected because no multipart boundary was found"
 
If I don't manually set the Content-type or Content-length, I get
corresponding errors as well.  Strangely, if I use the deprecated
MultipartPostMethod, I do not receive the error above and the upload
works for very small files.  However, for any reasonably sized file
(>1KB), I receive the following exception on the client side:
 
java.net.SocketException: Software caused connection abort: socket write
error
 at java.net.SocketOutputStream.socketWrite0(Native Method)
 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
 at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
 at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
 at
org.apache.commons.httpclient.WireLogOutputStream.write(WireLogOutputStr
eam.java:67)
 at
org.apache.commons.httpclient.methods.multipart.FilePart.sendData(FilePa
rt.java:222)
 at
org.apache.commons.httpclient.methods.multipart.Part.send(Part.java:311)
 at
org.apache.commons.httpclient.methods.multipart.Part.sendParts(Part.java
:384)
 at
org.apache.commons.httpclient.methods.multipart.Part.sendParts(Part.java
:358)
 at
org.apache.commons.httpclient.methods.MultipartPostMethod.writeRequestBo
dy(MultipartPostMethod.java:298)
 
 
I'm completely at a loss here...any ideas?
 
Thanks,
Jeff
 
 
 
  public static void main(String[] args) {
    ....
 
    PostMethod post = new PostMethod(servletPath);
 
    try {
      File tempFile = new File("C:\\0.txt");

      List<Part> parts = new ArrayList<Part>();
      parts.add(new FilePart("file", tempFile.getName(), tempFile));
      MultipartRequestEntity multipartRequestEntity = new
MultipartRequestEntity(parts.toArray(new Part[0]), post.getParams());
      post.setRequestEntity(multipartRequestEntity);
      post.setContentChunked(true);
      post.setRequestHeader("Content-type", "multipart/form-data");
      post.setRequestHeader("Content-length", "" +
multipartRequestEntity.getContentLength());
 
      // add parameters
      post.addParameter("description", "desc");
      ...
 
      post.setDoAuthentication(true);
 
      HttpClient client = new HttpClient();
      client.getState().setCredentials(
              new AuthScope(host, Integer.parseInt(port)),
              new UsernamePasswordCredentials(user, password));
      int status = client.executeMethod(post);
 
      if (HttpStatus.SC_OK != status) {
        String response = post.getResponseBodyAsString();
        System.out.println(attachmentGuid);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      post.releaseConnection();
    }
  }

RE: multipart post problem

Posted by "Segal, Jeffrey" <Je...@solers.com>.
Roland,

Thanks for replying.  I believe I discovered the root of my problem,
which caused me to try all sorts of things that got me into even more
trouble, including the parameter issue that you pointed out.  I found
that uploading any file of more than a few bytes caused the following
exception:

org.apache.commons.httpclient.HttpRecoverableException:
java.net.SocketException: Software caused connection abort: socket write
error

It turned out that the the server I was hitting (Weblogic 9.1) was
rejecting the request prematurely unless I added the Expect-Continue
header to my request, as in: 

 
post.getParams().setParameter(HttpMethodParams.USE_EXPECT_CONTINUE,
true);

After that, everything worked fine.

Thanks again,
Jeff

-----Original Message-----
From: Roland Weber [mailto:http-async@dubioso.net] 
Sent: Friday, October 27, 2006 1:47 PM
To: HttpClient User Discussion
Subject: Re: multipart post problem

Hello Jeffrey,

>   public static void main(String[] args) {
>     ....
>  
>     PostMethod post = new PostMethod(servletPath);
>  
>     try {
>       File tempFile = new File("C:\\0.txt");
> 
>       List<Part> parts = new ArrayList<Part>();
>       parts.add(new FilePart("file", tempFile.getName(), tempFile));
>       MultipartRequestEntity multipartRequestEntity = new 
> MultipartRequestEntity(parts.toArray(new Part[0]), post.getParams());
>       post.setRequestEntity(multipartRequestEntity);
>       post.setContentChunked(true);
>       post.setRequestHeader("Content-type", "multipart/form-data");
>       post.setRequestHeader("Content-length", "" + 
> multipartRequestEntity.getContentLength());

Do not add either of them.

>  
>       // add parameters
>       post.addParameter("description", "desc");
>       ...

Here, you are *replacing* the multipart request entity by a simple
URL-encoded description=desc name-value-pair. It's either one or the
other, multipart request entity or parameters. Add your name-
value-pairs to the multipart request entity, that's why it's called
multipart :-)

cheers,
  Roland


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


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


Re: multipart post problem

Posted by Roland Weber <ht...@dubioso.net>.
Hello Jeffrey,

>   public static void main(String[] args) {
>     ....
>  
>     PostMethod post = new PostMethod(servletPath);
>  
>     try {
>       File tempFile = new File("C:\\0.txt");
> 
>       List<Part> parts = new ArrayList<Part>();
>       parts.add(new FilePart("file", tempFile.getName(), tempFile));
>       MultipartRequestEntity multipartRequestEntity = new
> MultipartRequestEntity(parts.toArray(new Part[0]), post.getParams());
>       post.setRequestEntity(multipartRequestEntity);
>       post.setContentChunked(true);
>       post.setRequestHeader("Content-type", "multipart/form-data");
>       post.setRequestHeader("Content-length", "" +
> multipartRequestEntity.getContentLength());

Do not add either of them.

>  
>       // add parameters
>       post.addParameter("description", "desc");
>       ...

Here, you are *replacing* the multipart request entity by a simple
URL-encoded description=desc name-value-pair. It's either one or
the other, multipart request entity or parameters. Add your name-
value-pairs to the multipart request entity, that's why it's
called multipart :-)

cheers,
  Roland


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