2013-10-08 21 views
0

我想在Tomcat中开发一个Java servlet来处理post请求,最终打算处理文件并返回对客户端的响应中的所处理文件。根据我迄今为止的搜索结果,似乎我要处理的请求具有不同寻常的格式。我可以建议的替代品,但我不拥有控制权的要求的设计,这将如下所示:我应该如何解析POST请求消息的内容在Tomcat servlet中

POST /api/0/actions/image HTTP/1.1 
Host: <host>.com 
Content-Length: <content-length> 
Content-Type: multipart/form-data; boundary=<boundary> 
Accept-Encoding: gzip, deflate, compress 
Accept: */* 
User-Agent: <user-agent> 

--<boundary> 
Content-Disposition: form-data; name="application" 

{<authentication_info_as_JSON_object>} 
--<boundary> 
Content-Disposition: form-data; name="inputName" 

<inputname> 
--<boundary> 
Content-Disposition: form-data; name="options" 

{<options_as_JSON} 
--<boundary> 
Content-Disposition: form-data; name="input"; filename="<inputname>" 
Content-Type: <content-type> 

<file-binary> 
--<boundary>-- 

我可以使用正则表达式和标准的字符串处理方法,重建解析这个请求的内容为文本JSON对象和文件我需要手动处理。但这让我觉得这是一种容易出错并且难以维护的方法。那里有开源工具可以帮助我以更结构化或标准的方式处理此请求吗?

在此先感谢。

回答

2

这是如何不寻常的?

使用Apache Commons' FileUpload或同等产品。

如果你在一个Servlet 3.0容器,这是烤,例如,HttpServletRequest.getParts()

+0

对话与同事和寻找这个问题的答案我自己的时候缺乏的结果使我相信我们对这些请求做一些奇怪的事情。我想我只是没有找到正确的地方。我不知道为什么,但我一直很难找到我在Tomcat API中需要的东西。这是我在这里找的,谢谢。 –

相关问题