java - HttpUrlConnection and file issue -
java - HttpUrlConnection and file issue -
sorry bring again. when first started develop android app, using httpclient. realized google officially made class deprecated latest version of android; encourage developer utilize httpurlconnection instead.
i faced wall when tried upload file(jpeg) php server api hosted on gae. set lot of effort using multipart accomplish that. ended using next class. it's still not working unknown reason. php server api receives post info can't receive file. what`s wrong code?
in case you're wondering, can verify has nil server using isset($_files["profile_picture"])
verify i've received it.
here class:
package net.xxxxx; import android.util.log; import java.io.bufferedinputstream; import java.io.bufferedreader; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstream; import java.io.outputstreamwriter; import java.io.printwriter; import java.net.httpurlconnection; import java.net.url; import java.net.urlconnection; import java.util.arraylist; import java.util.list; import javax.net.ssl.httpsurlconnection; public class multipartutility { private httpurlconnection mconnection; private outputstream outputstream; private printwriter mwriter; private final string boundary; private static final string line_feed = "\r\n"; private final static string charset = "utf-8"; private final static string api_url = "https://select-connection.appspot.com/"; public static final string log_tag = "apirequesthandler"; public static final int connect_timeout = 15000; public static final int read_timeout = 10000; /** * ... */ public multipartutility() throws ioexception { // creates unique boundary based on time stamp. boundary = "===" + system.currenttimemillis() + "==="; url url = new url(api_url); mconnection = (httpsurlconnection)url.openconnection(); mconnection.setconnecttimeout(connect_timeout); mconnection.setreadtimeout(read_timeout); mconnection.setrequestmethod("post"); mconnection.setrequestproperty("accept-charset", "utf-8"); mconnection.setrequestproperty("accept", "application/json"); mconnection.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); mconnection.setusecaches (false); mconnection.setdoinput(true); mconnection.setdooutput(true); outputstream = mconnection.getoutputstream(); mwriter = new printwriter(new outputstreamwriter(outputstream, charset), true); } /** * ... */ public void addformfield(string name, string value) { mwriter.append("--" + boundary).append(line_feed) .append("content-disposition: form-data; name=\"" + name + "\"") .append(line_feed) .append("content-type: text/plain; charset=" + charset) .append(line_feed) .append(line_feed) .append(value) .append(line_feed); mwriter.flush(); } /** * ... */ public void addfilepart(string fieldname, file uploadfile) throws ioexception { string filename = uploadfile.getname(); mwriter.append("--" + boundary).append(line_feed) .append("content-disposition: form-data; name=\"" + fieldname + "\"; filename=\"" + filename + "\"") .append(line_feed) .append("content-type: " + urlconnection.guesscontenttypefromname(filename)) .append(line_feed) .append("content-transfer-encoding: binary") .append(line_feed) .append(line_feed); mwriter.flush(); fileinputstream inputstream = new fileinputstream(uploadfile); byte[] buffer = new byte[4096]; int bytesread; while ((bytesread = inputstream.read(buffer)) != -1) { outputstream.write(buffer, 0, bytesread); } outputstream.flush(); inputstream.close(); mwriter.append(line_feed); mwriter.flush(); } /** * ... */ public void addheaderfield(string name, string value) { mwriter.append(name + ": " + value).append(line_feed); mwriter.flush(); } /** * ... */ public string finish() throws ioexception { string response; mwriter.append(line_feed).flush(); mwriter.append("--" + boundary + "--").append(line_feed); mwriter.close(); // checks server's status code first int status = mconnection.getresponsecode(); if (status == httpurlconnection.http_ok) { response = apirequesthandler.convertstreamtostring(new bufferedinputstream(mconnection.getinputstream())); mconnection.disconnect(); } else { throw new ioexception("server returned non-ok status: " + status); } homecoming response; } }
and how utilize it:
try { multipartutility connection = new multipartutility(); connection.addformfield("action", "update_picture"); connection.addformfield("user_id", muserid); connection.addformfield("session_token", msessiontoken); connection.addfilepart("profile_picture", mprofilepicture); string result = connection.finish(); log.d("profile picture", result); } grab (ioexception e) { e.printstacktrace(); }
is multipart required? in case need send 1 file post request, can write binary info output stream of connection. (i have no experience php assume php able read request body). other fields transferred query parameters or http headers.
also recommend route http traffic through logging proxy fiddler2 see in http request body (and compare working test example).
java httpurlconnection multipart
Comments
Post a Comment