java - Using MultipartEntityBuilder to upload files -
i have existing java code upload files (as byte arrays) box.net, , thought upgrade code apache httpclient 4.4. however, has been proving little harder expected. here working legacy code:
httppost postmethod = new httppost(url); multipartentity multipartentity = new multipartentity(); (namevaluepair parameter : parameters) { string parametername = parameter.getname(); string parametervalue = parameter.getvalue(); stringbody valuebody = new stringbody(parametervalue, contenttype.multipart_form_data); multipartentity.addpart(parametername, valuebody); } iterator iterator = bytearrayhashmap.keyset().iterator(); while (iterator.hasnext()) { string key = (string) iterator.next(); byte[] bytearray = bytearrayhashmap.get(key); multipartentity.addpart(key, new bytearraybody(bytearray, key)); } postmethod.setentity(multipartentity);
and here new code:
httppost postmethod = new httppost(url); multipartentitybuilder multipartentitybuilder = multipartentitybuilder.create(); (namevaluepair parameter : parameters) { string parametername = parameter.getname(); string parametervalue = parameter.getvalue(); stringbody valuebody = new stringbody(parametervalue, contenttype.multipart_form_data); multipartentitybuilder.addpart(parametername, valuebody); } iterator iterator = bytearrayhashmap.keyset().iterator(); while (iterator.hasnext()) { string key = (string) iterator.next(); byte[] bytearray = bytearrayhashmap.get(key); multipartentitybuilder.addbinarybody(key, bytearray); } postmethod.setentity(multipartentitybuilder.build());
when run new code, server complains of missing parameter. using multipartentitybuilder correctly?
in case stumbles across question, able solve updating addbinarybody call set contenttype follows: multipartentitybuilder.addbinarybody(key, bytearray, contenttype.default_binary, key);
Comments
Post a Comment