简体   繁体   中英

how to sending multipart/form-data Post Request in with use of Apache HttpComponents in java

i am creating a desktop application which send file to an tomcat server. the servlet receiver and saves file fine.

I need some help to do a java program that post in a https site. I dont know how to put the parameters because it a multpart form data contect type.. Please help! when I do a post with firefox its like this...

This will depend. I've used the following technique to upload a multi-part file to a server before, based on providing a series of form key/name pairs.

This will be depended on you own requirements and what the servlet is actually expecting...

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

String name = file.getName();

entity.addPart(new FormBodyPart("someFormParameter", new StringBody("someFormName")));
/*...*/
entity.addPart("formFileNameParameter", new FileBody(file, mimeType));

HttpClient client = /*...*/

HttpPost post = new HttpPost(url.toURI());
post.setEntity(entity);
HttpResponse response = client.execute(post);

// Process response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM