使用 MultipartEntityBuilder 发送密钥和文件

Sending a key and file using MultipartEntityBuilder

我在网络方面没有太多经验,而且我的谷歌搜索技能似乎也没有比这更进一步。

我需要使用 "file" 作为 HTTP POST 密钥将文件发送到服务器。这是我所拥有的:

MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();  

   mpEntity.addBinaryBody("file", image);//set up the object to send

    HttpPut put = new HttpPut("http://address:port"); 

    put.setEntity(mpEntity.build());//put the object to be sent

    //try sending
    try {
        HttpResponse response = client.execute(put);

...

我在使用 InputStream 处理响应时收到 404 错误。服务器已启动并且 运行 并且在我从终端测试它时工作正常。

将文件的内容类型和名称添加到二进制正文中,如下所示:

mpEntity.addBinaryBody("file", image, ContentType.create("image/jpeg"), "image_name.jpg");