Java 无法从HttpClient获取完整的HTTP响应

Java 无法从HttpClient获取完整的HTTP响应,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我似乎没有从httpclient获得完整的json响应。我遇到了一个本地运行的api,如下所示: curl -i -X POST http://localhost:8098/<api location> -F "files=@<filename>" 我做错了什么?这是我的java代码。谢谢大家! public CloseableHttpResponse submit (File file) throws IOException { CloseableHttpCl

我似乎没有从httpclient获得完整的json响应。我遇到了一个本地运行的api,如下所示:

curl -i -X POST http://localhost:8098/<api location> -F "files=@<filename>"
我做错了什么?这是我的java代码。谢谢大家!

public CloseableHttpResponse submit (File file) throws IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost post = new HttpPost(API_LOCATION + API_BASE);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody("ISO xml file", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
    HttpEntity multipartEntity = builder.build();
    post.setEntity(multipartEntity);

    CloseableHttpResponse response = client.execute(post);
    System.out.println("response: " + IOUtils.toString(response.getEntity().getContent(),"UTF-8"));
    client.close();
    return response;
}

正如Andreas和Danilo在评论中提到的:

在curl中命名字段文件,但在Java中命名为isoxml文件。因为服务器只查找文件,所以它什么也看不到,并且什么也不响应。-


参数的名称呢?在curl上使用“文件”,在http客户端上使用“isoxml文件”。尝试将其更改为“文件”。-


我需要将“ISO xml文件”更改为“文件”,它就成功了。

在curl中,您将字段命名为
files
,但在Java中,您将其命名为
ISO xml文件
。由于服务器只查找
文件
,所以它什么也看不到,并且什么也不响应。那么参数的名称呢?在curl上使用“文件”,在http客户端上使用“isoxml文件”。试着把它改成“文件”。我把“ISO xml文件”改成了“文件”,效果很好。我没有意识到这个领域的重要性——我以为它只是为了鉴定身份。那么,那一行末尾的字符串有什么意义呢?我用file.getName()填充的字符串???
{"data":[]}
public CloseableHttpResponse submit (File file) throws IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost post = new HttpPost(API_LOCATION + API_BASE);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody("ISO xml file", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
    HttpEntity multipartEntity = builder.build();
    post.setEntity(multipartEntity);

    CloseableHttpResponse response = client.execute(post);
    System.out.println("response: " + IOUtils.toString(response.getEntity().getContent(),"UTF-8"));
    client.close();
    return response;
}