HttpServletResponse之getWriter()和getOutputStream()不能同时调用

利用ServletResponse的OutputStream可以向客户端返回字节流、输出文件。
本来想利用PrintWriter()向客户端输出提示信息,程序运行出错:
java.lang.IllegalStateException: getOutputStream() has already been called for this response

查看API:http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletResponse.html

HttpServletResponse之getWriter()和getOutputStream()不能同时调用_第1张图片

API中明确记载,在getWriter中的异常:
java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object

getOutputStream的异常:
java.lang.IllegalStateException - if the getWriter method has been called on this response

通知API可以得出结论:
getOutputStream()和getWriter()这两个方法互相排斥,调用了其中的任何一个方法后,就不能再调用另一方法。

你可能感兴趣的:(经验,学习,java基础)