当我尝试从HttpServletResponse获取Buffered Reader时告诉我已经调用了getInputStream,从而获得非法状态异常。 但是getReader是第一行代码,我根本就没有调用getInputStream ......
服务器:
@POST
@Path("/getReport")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_JSON)
public void getReport(@Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse, @Context UriInfo uriInfo )
throws ServletException, IOException{
JSONObject jobj = new JSONObject(new JSONTokener(servletRequest.getReader()));
答案 0 :(得分:2)
看起来restlet框架在Request对象上调用了getRequestEntityStream(),而该对象又调用了getInputStream(),因此在请求上调用getReader()会抛出IllegalStateException。
getReader()和getInputStream()的Servlet API文档说:
public java.io.BufferedReader getReader()
...
...
Throws:
java.lang.IllegalStateException - if getInputStream() method has been called on this request
public ServletInputStream getInputStream()
...
...
Throws:
java.lang.IllegalStateException - if the getReader() method has already been called for this request
从文档中我似乎无法在Request对象上同时调用getReader()和getInputStream()(即使我没有这样做)。
我不得不使用getInputStream()而不是getReader()。
答案 1 :(得分:1)
同时写入servletRequest.getReader()
和servletRequest.getInputStream()
是违法的。我认为servletRequest.getInputStream()
之前也会调用IllegalStateException
。