我正在使用apache HttpClient
来向服务器发送多个文件。以下是代码:
public static HttpResponse stringResponsePost(String urlString, String content, byte[] image,
HttpContext localContext, HttpClient httpclient) throws Exception {
URL url = new URL(URLDecoder.decode(urlString, "utf-8"));
URI u = url.toURI();
HttpPost post = new HttpPost();
post.setURI(u);
MultipartEntity reqEntity = new MultipartEntity();
StringBody sb = new StringBody(content, HTTP_CONTENT_TYPE_JSON, Charset.forName("UTF-8"));
ByteArrayBody ib = new ByteArrayBody(image, HTTP_CONTENT_TYPE_JPEG, "image");
reqEntity.addPart("interview_data", sb);
reqEntity.addPart("interview_image", ib);
post.setEntity(reqEntity);
HttpResponse response = null;
response = httpclient.execute(post, localContext);
return response;
}
问题在于,
MultipartEntity
类只有isChunked()
方法(始终返回false),如果我想启用分块编码来处理我的多部分实体,则没有“setChunked(boolean)”选项。我的问题是:
根据协议规范,HTTP多部分和分块是否可以共存?如果不能,为什么其他实体(如
InputStreamEntity
类)具有setChunked(boolean)
而MultipartEntity
没有?是否有任何方法可以启用分块并一次性上传多个文件,更好地使用Apache库?