Response对象的OutPutStream()和PrintWriteout()函数

来源:互联网 发布:枪战特效软件 编辑:程序博客网 时间:2024/05/21 18:40

区别

1.      getWriter() 用于向客户机回送字符数据

2.      getOutputStream() 返回的对象,可以回送字符数据,也可以回送字节数据(二进制数据)

OutputStream os=response.getOutputStream();

os.write("hello,world".getBytes());

 

如何选择:

如果我们是回送字符数据,则使用  PrintWriter对象 ,效率高

如果我们是回送字节数据(binarydate) ,则只能使用 OutputStream



OutPutStream和PrintWriteout这两个流不能同时使用.

比如:

OutputStreamos=response.getOutputStream();

        os.write("hello,world".getBytes());

        PrintWriterout=response.getWriter();

        out.println("abc");

就会报错:

java.lang.IllegalStateException: getOutputStream() has already been called for this response
 

不能同时使用printWriter和outputstream的原因

Web服务器会自动检查并关闭流

从该图,我们也可以看出. 为什么我们没有主动关闭流,程序也没有问题的原因.


0 0
原创粉丝点击