java 中的 MultipartEntityBuilder HttpPost 套接字写入错误

MultipartEntityBuilder HttpPost socket write error in java

我正在尝试在 java 中使用 httpPost 请求(通过 MultipartEntityBuilder)上传文件。但是我收到 软件导致连接中止:套接字写入错误

这是我的 httpPost 正文(在 wireShark 中)

------WebKitFormBoundaryWphJNFngxYSpEvNO
Content-Disposition: form-data; name="csrf_token"

csrf:sjwzV6dOZaNFwc0jWVrNNcFvhM7uv3BK00vZ0hCgEUzi2cG7r7Arx0Q3UZKlXeaR
------WebKitFormBoundaryWphJNFngxYSpEvNO
Content-Disposition: form-data; name="imagefilename"; filename="myfile.bin"
Content-Type: application/octet-stream

²qz‹ÁOOõMÓâg‘Ç`:----This area is file's binary code------Êëá‡/oåup

代码端是:

            File file = new File(filePath);
            String message = csrf_token;
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            builder.addBinaryBody("imagefilename", file, ContentType.DEFAULT_BINARY, file.getName());
            builder.addTextBody("csrf_token", message, ContentType.DEFAULT_BINARY);
//
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);

和错误:

有什么想法吗?谢谢大家。

我解决了这个问题,这些链接对回答很有用

here 是 fileupload with http Client example

here 也是文件上传,使用 httpUrlConnection