在会话中的 servlet 之间共享上传的文件
我可以将上传的文件保存为会话变量并在不同的 JSP/Servlet 之间共享吗? (即文件应该在上传页面以外的页面中可用。) 或者是否需要将文件保存在服务器中? 或者还有其他办法吗?
Can I hold a uploaded file as a session variable and share in between different JSP/Servlets?
(i.e the file should be available in pages other than the uploading page.)
Or is it necessary to save the file in the server?
Or is there any other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的。只需将其存储在
byte[]
中并将其保存为会话属性即可。然而,您需要意识到
byte[]
的每个字节
有效地占用了服务器内存的一个字节。因此,如果您有 100 个并发用户会话,每个会话只有一个 10MB 的大文件,那么就已经消耗了 1GB 的服务器内存。当您的服务器没有足够的内存并且有大量访问者时,您将面临OutOfMemoryError
的风险。在朝这个方向前进之前请三思。Yes, it's possible. Just store it in a
byte[]
and save it as a session attribute.You however need to realize that every
byte
of abyte[]
eats effectively one byte of server's memory. So if you have 100 simultaneous user sessions with each only one 10MB large file, then already 1GB of server memory is eaten away. You'll riskOutOfMemoryError
s when your server doesn't have sufficient memory and you've a lot of visitors. Think twice before you go in this direction.