Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using HttpClient and MultipartRequestEntity

807607Nov 10 2006 — edited Nov 13 2006
Hi!

I have checked everywhere for examples in this matter but I just cant make it work when using MultipartRequestEntity to send field values.
I just want to upload a file along with some parameter values.

Heres my code, quite simplified:

HttpClient client = new HttpClient();
File f = new File("C:\\licens.txt");
PostMethod mPost = new PostMethod("http://www.somepage.com");

Part[] parts = {new StringPart("id", "22"),
new FilePart(f.getName(), f) };

mPost.setRequestEntity( new MultipartRequestEntity(parts, mPost.getParams()) );
int code = client.executeMethod(mPost);
System.out.println( mPost.getResponseBodyAsString() );

The page that I send it to just print out all the parameters it gets. It also takes care of any file it may encounter but that part works just fine so I leave it out.

String tmp2 = "";
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements())
{
tmp2 = (String)paramNames.nextElement();
out.print(tmp2 + "-" + request.getParameter(tmp2) + "<br>\n");
}

The problem is that I just can�t get any of the parameters that I send on the recieving page and I cant make out why that is! When using other methods I can post just fine but not with this. The problem is that I just have to post the fields and the file at the same time.

Help is greatly appreciated!

//K
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 11 2006
Added on Nov 10 2006
1 comment
524 views