javafile-uploadhttp-postmultipartentityhttppostedfilebase

MultipartEntityBuilder HttpPost socket write error in java


I am trying file upload with httpPost request (by MultipartEntityBuilder) in java. But i get a Software caused connection abort: socket write error.

Here is my httpPost body (in 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

code side is:

            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);

and error:

enter image description here

Is there any idea? thanks for all.


Solution

  • i resolved the problem, thesee links are useful for answer

    here is fileupload with http Client example

    here is fileupload too, with httpUrlConnection