【问题标题】:MultipartEntityBuilder HttpPost socket write error in javaJava中的MultipartEntityBuilder HttpPost套接字写入错误
【发布时间】:2016-02-17 07:25:43
【问题描述】:

我正在尝试使用 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);

和错误:

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

【问题讨论】:

    标签: java file-upload http-post multipartentity httppostedfilebase


    【解决方案1】:

    我解决了问题,查看链接对回答很有用

    here 是带有 http 客户端示例的文件上传

    here 也是文件上传,带有 httpUrlConnection

    【讨论】:

      猜你喜欢
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多