Share uploaded file between servlets in session
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 OutOfMemoryError
s when your server doesn't have sufficient memory and you've a lot of visitors. Think twice before you go in this direction.
精彩评论