java 发送上传文件的 http put 请求

public static CloseableHttpResponse putFilesWithHeader(String url,JSONObject requestJson,Map header ,File... files) {
    CloseableHttpResponse execute = null;

    HttpPut httpPut = new HttpPut(url);
    String responseStr = null;
    if (header != null) {
        for (Map.Entry entry : header.entrySet()) {
            httpPut.setHeader(entry.getKey(),entry.getValue());
        }
    }
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName("UTF-8"));
    for (File file : files) {
        multipartEntityBuilder.addBinaryBody("facePic", file);
    }
    Set> entries = requestJson.entrySet();
    for (Map.Entry requestParam : entries ) {
        multipartEntityBuilder.addTextBody(requestParam.getKey(),(String) requestParam.getValue());
    }
    HttpEntity entiy = multipartEntityBuilder.build();
    httpPut.setEntity(entiy);
    CloseableHttpClient  closeableHttpClient = HttpClients.createDefault();
    try {
          execute = closeableHttpClient.execute(httpPut);
    } catch (Exception e) {
        e.printStackTrace();
    }
       return  execute;


}

你可能感兴趣的:(http,put)