开发者

Share uploaded file between servlets in session

开发者 https://www.devze.com 2023-04-04 10:12 出处:网络
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.)

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?


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

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消