As per the PartSource Javadocs, PartSource.createInputStream() can be called more than once. Each call is expected to return a fresh InputStream. PartSource behaves this way as there are some cases where POST content must be sent more than once.
Here are a few things you can try to avoid this problem:
- if you are using one pass authentication (BASIC only I think) then setting preemptive authentication should take care of the problem. Try HttpClient.getState().setAuthenticationPreemptive(true)
- another option is to use the "Expect: 100-continue" header. This feature requires HTTP 1.1 and is not well supported by all web servers. Try HttpMethod.setRequestHeader("Expect", "100-continue")
If neither of these things work you will have to recreate the InputStream or buffer the content somewhere.
Mike
David Sean Taylor wrote:
I created a little class called InputStreamPartSource to wrapper an input stream as a part source.
MultipartPostMethod post = new MultipartPostMethod("http://192.168.1.4:8080/someServlet");
InputStreamPartSource source = new InputStreamPartSource(stream, "business.xml");
FilePart part = new FilePart("business.xml", source);
post.addPart(part);
int status = client.executeMethod(post);
It seems that the abstract class Part is calling createInputStream twice on my source.
Why does it need to create the stream twice?
This causes a bad file descriptor since the stream was already closed after the first call.
The stream is an expensive resource which comes from a CMS system and I prefer not to create it twice.
Is this a bug are normal/required behavior?
InputStreamPartSource Class included below
public class InputStreamPartSource implements PartSource
{
InputStream stream = null;
String filename = null;
int length = 0;
public InputStreamPartSource(InputStream stream, String filename, int length)
{
this.stream = stream;
this.filename = filename;
this.length = length;
}
/* (non-Javadoc)
* @see org.apache.commons.httpclient.methods.multipart.PartSource#createInputSt ream()
*/
public InputStream createInputStream()
throws IOException
{
return this.stream;
}
/* (non-Javadoc)
* @see org.apache.commons.httpclient.methods.multipart.PartSource#getFileName()
*/
public String getFileName()
{
return this.filename;
}
/* (non-Javadoc)
* @see org.apache.commons.httpclient.methods.multipart.PartSource#getLength()
*/
public long getLength()
{
return this.length;
}
-- David Sean Taylor Bluesunrise Software [EMAIL PROTECTED] +01 707 773-4646
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]