android - POST a file with other form data using HttpURLConnection -
im trying post file api of mine along other parameters.
eg. post /media parameters
filename = 'test.png'
file = -the-actual-file-
i can postman (using form-data), api side of things fine.
here android code using httpurlconnection:
namevaluepairs.add(new basicnamevaluepair("filename", "test.png")); url object = new url(url); httpurlconnection connection = (httpurlconnection) object.openconnection(); connection.setreadtimeout(60 * 1000); connection.setconnecttimeout(60 * 1000); string auth = username+":"+password; byte[] data = auth.getbytes(); string encodeauth = "basic " + base64.encodetostring(data, base64.default); connection.setdoinput(true); connection.setdooutput(true); connection.setusecaches(false); connection.setrequestproperty("authorization", encodeauth); connection.setrequestproperty("accept", accept); connection.setrequestmethod("post"); connection.setrequestproperty("enctype", "multipart/form-data"); connection.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); dataoutputstream = new dataoutputstream(connection.getoutputstream()); writer = new bufferedwriter(new outputstreamwriter(dataoutputstream, "utf-8")); writer.write(getquery(namevaluepairs)); writer.write("&file=" + "image.jpg"); writer.write file file = getfile(item); if (file == null) { log.e("uploadfile", "source file not exist " ); } else { addfilepart("file", file); } } writer.flush(); writer.close(); dataoutputstream.close(); connection.connect();
android multipart upload.
public string multipartrequest(string urlto, map<string, string> parmas, string filepath, string filefield, string filemimetype) throws customexception { httpurlconnection connection = null; dataoutputstream outputstream = null; inputstream inputstream = null; string twohyphens = "--"; string boundary = "*****" + long.tostring(system.currenttimemillis()) + "*****"; string lineend = "\r\n"; string result = ""; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024 * 1024; string[] q = filepath.split("/"); int idx = q.length - 1; try { file file = new file(filepath); fileinputstream fileinputstream = new fileinputstream(file); url url = new url(urlto); connection = (httpurlconnection) url.openconnection(); connection.setdoinput(true); connection.setdooutput(true); connection.setusecaches(false); connection.setrequestmethod("post"); connection.setrequestproperty("connection", "keep-alive"); connection.setrequestproperty("user-agent", "android multipart http client 1.0"); connection.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); outputstream = new dataoutputstream(connection.getoutputstream()); outputstream.writebytes(twohyphens + boundary + lineend); outputstream.writebytes("content-disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + "\"" + lineend); outputstream.writebytes("content-type: " + filemimetype + lineend); outputstream.writebytes("content-transfer-encoding: binary" + lineend); outputstream.writebytes(lineend); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { outputstream.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } outputstream.writebytes(lineend); // upload post data iterator<string> keys = parmas.keyset().iterator(); while (keys.hasnext()) { string key = keys.next(); string value = parmas.get(key); outputstream.writebytes(twohyphens + boundary + lineend); outputstream.writebytes("content-disposition: form-data; name=\"" + key + "\"" + lineend); outputstream.writebytes("content-type: text/plain" + lineend); outputstream.writebytes(lineend); outputstream.writebytes(value); outputstream.writebytes(lineend); } outputstream.writebytes(twohyphens + boundary + twohyphens + lineend); if (200 != connection.getresponsecode()) { throw new customexception("failed upload code:" + connection.getresponsecode() + " " + connection.getresponsemessage()); } inputstream = connection.getinputstream(); result = this.convertstreamtostring(inputstream); fileinputstream.close(); inputstream.close(); outputstream.flush(); outputstream.close(); return result; } catch (exception e) { logger.error(e); throw new customexception(e); } } private string convertstreamtostring(inputstream is) { bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { sb.append(line); } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return sb.tostring(); }
calling code:
//setup params map<string, string> params = new hashmap<string, string>(2); params.put("foo", hash); params.put("bar", caption); string result = multipartrequest(url_upload_video, params, pathtovideofile, "video", "video/mp4"); //next parse result string
Comments
Post a Comment