Httpclient simulates postman to send a request?

problem description

the post method calls the remote interface, and postman succeeds, but it fails to use httpclient request through java code.
the server cannot debug and does not know its own error A server error occurred.. Please contact the administrator

the environmental background of the problems and what methods you have tried

  1. first you need to convert multipartfile to file
  2. In the entity of the httpclient constructed by
  3. , MultipartEntity
  4. is used.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

request using postman

postman construction request succeeded
clipboard.png

postman

headercontent-type form-data , "A server error occurred. Please contact the administrator."
clipboard.png

controller layer

@ RequestMapping (value = "/ image", method = {RequestMethod.POST})

@ResponseBody
public String getSimilarImage( int num, MultipartFile image) {

    HashMap<String, String> params = new HashMap<>();
    params.put(ConstantUtil.NUM, String.valueOf(num));

    String result = null;
    File file = null;
    try {

        // 
        file = new File(image.getOriginalFilename());
        FileUtils.copyInputStreamToFile(image.getInputStream(),file);

        result = HttpClientUtil.doFilePost(gunsProperties.getSimilarImage(), file, params);
        System.out.println(result);
    } catch (Exception e) {
        logger.debug(e.getMessage());
    }

    File del = new File(file.toURI());
    del.delete();
    return result;

}

the utility class post method of httpclient

public static String doFilePost (String url, File file, HashMap < String, String > params) {

    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    String resultString = "";
    try {
        httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().
                setConnectTimeout(2000000000).
                setConnectionRequestTimeout(2000000000).
                setSocketTimeout(2000000000).build();
        // Http Post
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(ConstantUtil.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE_VALUE);
        httpPost.setConfig(requestConfig);
        /*httpPost.setProtocolVersion(HttpVersion.HTTP_1_0);
        httpPost.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);*/

        // Header
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addPart("image", new FileBody(file));

        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                StringBody contentBody = new StringBody(entry.getValue(),
                        "text/plain", Charset.forName("UTF-8"));
                builder.addPart(entry.getKey(), contentBody);
            }
        }

        HttpEntity entity = builder.build();

        httpPost.setEntity(entity);
        response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();
        if (responseEntity != null) {
            resultString = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
        }
    } catch (Exception e) {
        logger.debug(",{},{}", e, e.getMessage());
        MsgResponse.failMessage(ConstantUtil.OUT_OF_DATE);
    } finally {
        try {
            if (response != null) {
                response.close();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        httpClient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return resultString;
}

what result do you expect? What is the error message actually seen?

expect the same result as postman. I don"t know which link in the middle has the wrong setting


the reason is that when you cannot set header, to use multipartfile in post, you already use

by default.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e78532-464a4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e78532-464a4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?