可靠的数据服务
How can i make sure my file serving is reliable and scalable? How many parallel request it can handle?
I am thinking beyond the hardware capability and band width.
i am following Streaming large files in a java servlet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这些是静态文件,只需直接链接即可。所有像样的 servlet 容器/应用程序服务器都有一个完善的
DefaultServlet
。如果这些静态文件位于您将它们链接到的 Web 应用程序外部,那么您也可以将这些文件的根文件夹添加为另一个上下文。目前尚不清楚您正在使用哪个服务器,但如果它是 Tomcat,您只需将新的
添加到server.xml
即可:这样就可以通过 <代码>http://example.com/files/...。
如果这些是动态生成的文件或来自数据库的文件,那么您需要开发一个有效执行 IO 工作的 servlet:即不必将整个数据存储在内存中(例如,在 ByteArrayInputStream 或
byte[]
在将它们发送到输出之前,只需将字节立即写入输出即可。您可能会发现这些 基本 fileservlet 和更多 高级fileservlet(支持简历等)很有用。If those are static files, just link to it directly. All decent servletcontainers/appservers have a well-developed
DefaultServlet
. If those are static files located outside the webapplication from where you'd link them to, then you can also just add the root folder of those files as another context. It's unclear which server you're using, but if it were Tomcat, you could just add a new<Context>
toserver.xml
:This way it's accessible by
http://example.com/files/...
.If those are dynamically generated files or files coming from a database, then you need to develop a servlet which does the IO job efficiently: i.e. do not unnecessarily store the entire data in memory (e.g. in a
ByteArrayInputStream
orbyte[]
before emitting them to the output. Just write the bytes immediately to the output as it comes in. You may find this those examples of a basic fileservlet and a more advanced fileservlet (supporting resumes and so on) useful.如果您只是从文件系统提供静态文件,只需使用 Apache - 它会比您自己编写的任何内容更好。
If you are just serving static files from a file system just use Apache - it's going to be better then anything you will write yourself.