接收由Apache HttpClient和MultipartEntity发布的二进制数据

时间:2014-03-11 11:29:31

标签: java servlets apache-commons

服务器在post请求中从客户端接收一些数据。 为了发送这些数据,我使用这样的smth:

HttpPost post = new HttpPost(_config.url);
MultipartEntity entity = new MultipartEntity();
for (Entry<String, ContentBody> e : params.entrySet()) {
    entity.addPart(e.getKey(), e.getValue());
}
post.setEntity(entity);

当服务器接受请求时,数据存储在请求体中。如果我将它读作char数组并将其转换为字符串,我可以看到这个信息:

--ltiKmQX6YRvytGyyKS_F3Zz__tVbvbQktQV
Content-Disposition: form-data; name="file"; filename="art21.jpg.0"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

ÿØÿàJFIFHHÿÛC
2!!22222222222222222222222222222222222222222222222222ÿOX"ÿÄ

但是你可以看到请求包含几个参数:filename和file。 如何从请求正文中获取这些参数?

1 个答案:

答案 0 :(得分:0)

终于找到了解决方案:

要从post body获取数据二进制数据,我使用来自apache的ServletFileUpload:

List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);