我正在使用Apache http客户端,使用cURL,命令将是:
curl -i -X POST --data-binary @data.csv -H "Content-Type:text/csv" "http://localhost:8080/"
Java(Apache Http Client)中的等价物是什么?我尝试了以下方法:
HttpPost postRequest = new HttpPost("...");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
File file = new File("data.csv");
builder.addBinaryBody("upfile", file, ContentType.create("text/csv"), "data.csv");
HttpEntity entity = builder.build();
postRequest.setEntity(entity);
client.execute(postRequest);
curl
直接将文件内容作为正文(实体)发送,而不是多部分。尝试
HttpPost post = new HttpPost(url);
post.setEntity(new FileEntity(new File(filename),ContentType.create("text/csv")));
... client.execute(post) ...
PS:curl -d/--data[-*]
已经使用POST,你不需要-X
。