java - How to upload a file using Apache HttpPost -
i've got curl call this:
curl -i -x post -h "content-type: multipart/form-data" -f "file=@data_test/json_test.json" http://domain.com/api/upload_json/
all need java implementation call. i've made code, file, appears server, seems null.
public static void uploadjson(string url, file jsonfile) { try { httppost request = new httppost(url); entitybuilder builder = entitybuilder .create() .setfile(jsonfile) .setcontenttype(contenttype .multipart_form_data) .chunked(); httpentity entity = builder.build(); request.setentity(entity); httpresponse response = gethttpclient().execute(request); logger.info("response: {}", response.tostring()); } catch (ioexception e) { logger.error(e.getmessage()); } }
what proper way build request?
closeablehttpclient httpclient = httpclientbuilder.create() .build(); httpentity requestentity = multipartentitybuilder.create() .addbinarybody("file", new file("data_test/json_test.json")) .build(); httppost post = new httppost("http://domain.com/api/upload_json/"); post.setentity(requestentity); try (closeablehttpresponse response = httpclient.execute(post)) { system.out.print(response.getstatusline()); entityutils.consume(response.getentity()); }
Comments
Post a Comment