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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -