- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 Java Servlet 中,可以通过 response.getOutputStream()
或 response.getWriter()
访问响应正文。是否应该在写入此 OutputStream
后调用 .close()
?
一方面,Blochian 劝告总是关闭 OutputStream
s。另一方面,我不认为在这种情况下存在需要关闭的底层资源。套接字的打开/关闭在 HTTP 级别进行管理,以允许诸如持久连接之类的事情。
最佳答案
通常您不应关闭流。作为 servlet 请求生命周期的一部分,servlet 容器将在 servlet 完成运行后自动关闭流。
例如,如果您关闭了流,如果您实现了 Filter,它将不可用。 .
说了这么多,如果你关闭它,只要你不尝试再次使用它,什么都不会发生。
EDIT2:adrian.tarau 是正确的,如果你想在 servlet 完成它的事情后改变响应,你应该创建一个扩展 HttpServletResponseWrapper 的包装器并缓冲输出。这是为了防止输出直接发送到客户端,但也允许您保护 servlet 是否关闭流,根据以下摘录(强调我的):
A filter that modifies a response must usually capture the response before it is returned to the client. The way to do this is to pass the servlet that generates the response a stand-in stream. The stand-in stream prevents the servlet from closing the original response stream when it completes and allows the filter to modify the servlet's response.
从那篇 Sun 官方文章中可以推断,从 servlet 中关闭 OutputStream
是正常现象,但不是强制性的。
关于java - 是否应该在 HttpServletResponse.getOutputStream()/.getWriter() 上调用 .close()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1159168/
我的 Controller 中有这个方法。 Whick 基本上呈现 pdf 文件。 def getUserInvoiceImage (GetInvoiceDataCommand form) {
我用谷歌搜索错误消息 getOutputStream() has already been called for this response很多人说这是因为 ,但在我的代码中,没有空格或换行符。我在
我用谷歌搜索错误消息 getOutputStream() has already been called for this response很多人说这是因为 ,但在我的代码中,没有空格或换行符。我在
这个问题已经有答案了: java.lang.IllegalStateException: getOutputStream() has already been called for this resp
我正在使用 Spring 开发一个项目,我遇到了这个问题,我用谷歌搜索了错误消息,我确实找到了解决方案,甚至所有关于这个问题的 SO 帖子也是如此 java.lang.IllegalStateExce
你好,当我尝试在程序的连接方法中使用这两行代码时,出现错误“找不到符号方法 getOutputStream()” 我不知道我做错了什么,这是代码 socket = new ServerSocket(6
我用谷歌搜索错误信息 getOutputStream() has already been called for this response很多人说是因为 ,但在我的代码中,没有空格或换行符。我在li
我正在编写一个 java 代码来将 POST 请求发送到 URL,但它卡在粗体指示的行上。我在 Android 模拟器上运行这个 URL url = new URL("htt HttpURLConne
来自此链接“Writing to the OutputStream of Java Process/ProcessBuilder as pipe”并进行研究,我没有找到指定从 getOutputStr
我正在使用 jssc 库通过串行端口与设备进行通信。在标准 java SerialComm 库中有两个方法 getInputStream() 和 getOutputStream()。 为什么我需要这个
我正在尝试构建将数据发布到node.js服务器的java应用程序。 在 Node.js 中处理帖子的代码如下: app.post('/login.html', function (req, res)
我有一个想要测试的servlet。我想测试 Mockito 的 doGet。在来自 srvlet 的主代码信息中,通过 PrintWriter 转到页面/json。在我的测试中,我想要一个包含测试信息
我正在为我们的 Android 设备创建一个应用程序。本节的目的是将用户名和密码(当前仅分配为字符串)发布到 Web 服务并接收登录 token 。当运行代码时,在 getOutputStream()
我正在尝试从 MySQL-DB 下载 pdf(我将其作为 Blob 获取,到目前为止一切正常。但是当我尝试让 ServletOutputstream 将其发送到客户端时,程序崩溃。 AFAIK 当该方
所以我从 GUI 启动 Bukkit (Minecraft) 服务器。 ProcessBuilder builder = new ProcessBuilder(); builder.redirectE
我在 Windows 机器上使用 gzip 实用程序。我压缩了一个文件并作为 blob 存储在数据库中。当我想使用 gzip 实用程序解压缩此文件时,我将此字节流写入 process.getOutpu
我知道还有许多其他帖子处理同样的错误,但所有这些帖子要么是关于 JSP/GSP 页面的,要么是出于任何其他原因,对我的情况不是很有帮助。我将 Spring MVC 与 Thymeleaf 一起使用。以
一个问题 以为例 DataOutputStream output= new DataOutputStream(clientSocket.getOutputStream()) ; 或 DataInput
我正在使用 Spring @RestController 并以 Json 格式发送响应。这工作正常,但我需要向记录器发送响应,所以我已经实现了 Spring 的 HandlerInterceptor
Socket socket = sslSocketFactory.createSocket(); socket.connect(new InetSocketAddress(hostname, port
我是一名优秀的程序员,十分优秀!