response.reset() 與response.resetbuffer使用場景


版權聲明:本文為博主原創文章,遵循 CC 4.0 by-sa 版權協議,轉載請附上原文出處鏈接和本聲明。
原文鏈接:https://blog.csdn.net/XingKong22star/article/details/39207015

getResponse的getWriter()方法

getResponse的getWriter()方法連續兩次輸出流到頁面的時候,第二次的流會包括第一次的流,所以可以使用response.reset或者resetBuffer的方法。

 

reset():
Clears any data that exists in the buffer as well as the status code and headers. If the response has been committed, this method throws an IllegalStateException.

response.reset()的使用有一個條件受限:response的任何打開流關閉之后都不能再reset .

 

resetBuffer():
Clears the content of the underlying buffer in the response without clearing headers or status code. If the response has been committed, this method throws an

IllegalStateException.

可以看到resetBuffer方法與reset方法的區別是,頭和狀態碼沒有清除。


一般用於下載文件excel操作時:
空白行的出現原因,jsp代碼編譯后產生。就是有jsp生成html文件的時候,html文件內部會出現很多空白行。下載后的文件內的空白行也是這樣產生的。
因此,需要 response.reset() 來清除首部的空白行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM