playframework 2.0 - Play 2.0 How to Post MultipartFormData using WS.url or WS.WSRequest -


in java http request, can make multipart http post.

httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url);  filebody bin = new filebody(new file(filename)); stringbody comment = new stringbody("filename: " + filename);  multipartentity reqentity = new multipartentity(); reqentity.addpart("bin", bin); reqentity.addpart("comment", comment); httppost.setentity(reqentity);  httpresponse response = httpclient.execute(httppost); httpentity resentity = response.getentity(); 

how achieve same using ws.url or ws.wsrequest?

wsrequestholder wsreq = ws.url("http//url");             wsreq.setheader("content-type", "multipart/form-data"); 

this sloppy, , can cleaned up, here's did working. feel free make better.

import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream;  import play.libs.ws;  import com.ning.http.multipart.filepart; import com.ning.http.multipart.multipartrequestentity; import com.ning.http.multipart.part;  bytearrayoutputstream bos = new bytearrayoutputstream();  // build multiparts list<part> parts = new arraylist<>(); parts.add(new filepart("file", new file(filename))); part[] partsa = parts.toarray(new part[parts.size()]);  // add multipartrequestentity multipartrequestentity reqe = new multipartrequestentity(partsa, null); reqe.writerequest(bos); inputstream reqis = new bytearrayinputstream(bos.tobytearray()); ws.wsrequestholder req = ws.url(interchangeconfig.conflateurl+"dataset")     .setcontenttype(reqe.getcontenttype()); req.post(reqis).map(...); // or req.post(reqis).get(); 

this using pieces in play 2.0 framework.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -