java 从 HttpServletRequest 对象获取已发布的 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5453649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Posted XML from HttpServletRequest Object
提问by c12
I have a Filter that receives the HttpServletRequest and the request is a POST that consists of an xml that I need to read into my filter method. What is the best way to get the posted xml from the HttpServletRequest object.
我有一个接收 HttpServletRequest 的过滤器,请求是一个 POST,它包含一个我需要读入过滤器方法的 xml。从 HttpServletRequest 对象获取已发布的 xml 的最佳方法是什么。
采纳答案by BalusC
That depends on how the client has sent it.
这取决于客户端如何发送它。
If it's been sent as the raw request body, then use ServletRequest#getInputStream()
:
如果它作为原始请求正文发送,则使用ServletRequest#getInputStream()
:
InputStream xml = request.getInputStream();
// ...
If it's been sent as a regular application/x-www-form-urlencoded
request parameter, then use ServletRequest#getParameter()
:
如果它作为常规application/x-www-form-urlencoded
请求参数发送,则使用ServletRequest#getParameter()
:
String xml = request.getParameter("somename");
// ...
If it's been sent as an uploaded file in flavor of a multipart/form-data
part, then use HttpServletRequest#getPart()
.
如果它是作为上传文件以multipart/form-data
部分风格发送的,则使用HttpServletRequest#getPart()
.
InputStream xml = request.getPart("somename").getInputStream();
// ...
That were the ways supported by the standard servlet API. Other ways may require a different or 3rd party API (e.g. SOAP).
这是标准 servlet API 支持的方式。其他方式可能需要不同的或第 3 方 API(例如 SOAP)。