简体   繁体   English

当我从servlet向浏览器发送文件时,输出流会发生什么?

[英]What happens to the output stream when I'm sending a file from a servlet to a browser?

I have an application which is used by users to download some files that are retrived unfortunately via a webservice.The webservice provide the content of the files inside the payload :( 我有一个应用程序,用户可使用该应用程序下载一些不幸通过Web服务检索的文件.Web服务在有效载荷内提供文件内容:(

So i might have a lot of documents simultaneously in the memory of the webapp thing that leads to out of memory. 因此,我可能同时在webapp的内存中有很多文档,导致内存不足。

The question is : when the user press on the webage download I start writing on the outputstream...but the user receives a popup with "save" or "cancel". 问题是:当用户按下webage下载时,我开始在outputstream上写...但是用户收到带有“保存”或“取消”的弹出窗口。 What happens with the document until he press save ? 在按保存之前,文档将如何处理? What if he press cancel ? 如果按取消键怎么办?

The output stream is blocked on write until the user press save ? 输出流在写入时被阻止,直到用户按Save?

Thanks... 谢谢...

No, the outputstream is not locked, it's buffered and it's sent to the client when your servlet container thinks it's the right time (or when you force sending with a flush() call on it). 不,输出流没有锁定,而是在您的servlet容器认为合适的时候(或者在您强制使用flush()调用发送时)将其缓冲并发送到客户端。

Depending on your server infrastructure (if you have or not a reverse proxy in front of your web server) your request may fail or not, but usually you don't have to care about this. 根据您的服务器基础结构(如果您的Web服务器前面是否有反向代理),您的请求可能会失败还是不会失败,但是通常您不必担心。

And regarding the memory issue, you should probably call the web and either write directly as you read from it to the request output stream or you should write this to a file and then write this file in chunks to the response to avoid using too much memory and bringing the server down due to out of memory errors. 关于内存问题,您可能应该调用Web并从其读取时直接将其写入请求输出流,或者应该将此文件写入文件,然后将此文件分块写入响应中,以避免使用过多的内存并由于内存不足错误而导致服务器停机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM