Send text and image to php with java -
this first question on stackoverflow, if have comment on way put/explain it, gladly accept pointers.
so here is:
i want sent both text , image php script on server. can succesfully sent both apart each other, not together. till use string:
string datasend = urlencoder.encode("user","utf-8") +"="+urlencoder.encode(username,"utf-8"); url url = new url(urlcopyimage); urlconnection connection = url.openconnection(); connection.setdooutput(true); outputstreamwriter out = new outputstreamwriter(connection.getoutputstream()); out.write(datasend); out.close();
for image use:
url url = new url(urlcopyimage); urlconnection connection = url.openconnection(); connection.setdooutput(true); imageio.write(image,"png", connection.getoutputstream());
can somehow combine these two?
in php script merely use $_post
receive string , image use:
$incomingdata = file_get_contents('php://input');
any tips welcome.
for record, use php file create file containing image on server. username useful specifying directory.
edit:
thanks answers have been able make work, though don't quite understand yet.
multipartentity multipart = new multipartentity(); multipart.addpart("user", new stringbody(username)); bytearrayoutputstream os = new bytearrayoutputstream(); imageio.write(image, "png", os); inputstream = new bytearrayinputstream(os.tobytearray()); contentbody cb = new inputstreambody(is, "something.png"); multipart.addpart("image", cb); httppost post = new httppost(urlcopyimage); post.setentity(multipart); httpclient client = new defaulthttpclient(); httpresponse response = client.execute(post);
i handing contentbody inputstreambody bytestream imageio. that's great. don't second parameter of inputstreambody supposed do. although works, feels not right.
btw, php code:
$user = $_post['user']; copy($_files['image']['tmp_name'],$filename);
i grateful if explain i'm doing here. help!
use apache http client make post request mime type multipart/form-data, , add parts both binary data , text.
on php side, can read file via $_file
see blog post wrote time ago http://zybnet.com/java-http-client-and-post-requests/
Comments
Post a Comment