要发送二进制数据,您需要使用的
addBinaryBody方法
MultipartEntityBuilder。附件样本:
import org.apache.http.entity.ContentType;import org.apache.http.entity.mime.MultipartEntityBuilder;//Image attachingMultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();File file;multipartEntity.addBinaryBody("someName", file, ContentType.create("image/jpeg"), file.getName());//Json string attachingString json;multipartEntity.addPart("someName", new StringBody(json, ContentType.TEXT_PLAIN));然后像往常一样发出请求:
HttpPut put = new HttpPut("url");put.setEntity(multipartEntity.build());HttpResponse response = client.execute(put);int statusCode = response.getStatusLine().getStatusCode();


