您好,我对 HTTP Post 不太了解。我需要将文件作为键值对发布到 url。示例:
key = "logFile", value = fileToUpdate
我尝试向谷歌学习,并想出了这段代码:
try {
File filePost = new File("C:/Users/harun.acar/IdeaProjects/RaspBerryTest/540624d27037e20e3829cc03-136_rows-5ac490bf.bin");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url+"?"+params);
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addPart("file",new FileBody(filePost));
httpPost.setEntity(entity.build());
HttpResponse response = httpClient.execute(httpPost);
String responseStr = EntityUtils.toString(response.getEntity());
System.out.println(responseStr);
}catch (Exception e){
System.out.println(e.toString());
}
但是我需要指定关键参数,但我不知道如何将其放入条目中(或者是否需要将其放在其他地方)感谢您的帮助。
最佳答案
您将使用此行添加 key :
entity.addPart("file",new FileBody(filePost));
您必须将字符串“file”
更改为“logFile”
您也可以在不使用FileBody
类的情况下调用此方法
entity.addBinaryBody("logFile", filePost);
关于java - 如何在Java中传递带有关键参数的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34724259/