在会话中的Servlet之间共享上载的文件 [英] Share uploaded file between servlets in session

查看:51
本文介绍了在会话中的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?

推荐答案

是的,有可能.只需将其存储在byte[]中并将其另存为会话属性即可.

Yes, it's possible. Just store it in a byte[] and save it as a session attribute.

byte[] fileContent = getItSomehow();
session.setAttribute("fileContent", fileContent);
// ...

但是,您需要认识到byte[]的每个byte实际上都占用了服务器内存的一个字节.因此,如果您有100个同时进行的用户会话,而每个会话只有一个10MB的大文件,那么已经耗尽了1GB的服务器内存.当服务器的内存不足并且访问者很多时,您将冒OutOfMemoryError的风险.在朝这个方向前进之前,请三思.

You however need to realize that every byte of a byte[] 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 risk OutOfMemoryErrors when your server doesn't have sufficient memory and you've a lot of visitors. Think twice before you go in this direction.

这篇关于在会话中的Servlet之间共享上载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆