从Spring MVC流式传输动态文件 [英] Streaming Dynamic Files from Spring MVC

查看:98
本文介绍了从Spring MVC流式传输动态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Web MVC应用程序(还有一个BlazeDS应用程序,尽管不那么相关),其中文件是根据某些客户端操作动态生成的.

I've got a Spring Web MVC application (and also a BlazeDS application, though not as relevant) where files are dynamically generated based on certain client actions.

我只想将文件系统上的某个目录映射到Spring MVC(或应用服务器)URL,并让它为该目录中的文件提供服务(具有流和标准的最后修改的标头支持).理想情况下,映射目录将通过spring config进行配置,因为我已经支持每台机器进行设置了.

I'd like to just map a certain directory on the file system to Spring MVC (or the app server) url and let it serve the files in that directory (with streaming and standard last-modified header support). Ideally, the mapped directory would be configured via the spring config, since I already have support per-machine for setting that up.

那么,我该怎么做?到目前为止,我能找到的最好的方法是编写一个控制器,该控制器可以手动读取文件并逐字节地传输文件.但是,这似乎远非理想.对这样的支持是否已经纳入Spring MVC或标准应用服务器规范中?

So, how can I do this? The best I can find so far is to write a controller that reads the file manually and streams it byte-by-byte. However, that seems far less than ideal. Is support for something like this already baked into Spring MVC or the standard application server spec?

谢谢!

推荐答案

如果您的处理模型支持它,为什么不将文件系统的中间人完全从图片中删除,然后将它们按原样通过响应流流回去产生了吗?看一下Spring MVC的AbstractExcelView和AbstractPDFView类,看看如何完成此操作的一些示例.

If your processing model supports it, why not cut the middleman of the filesystem out of the picture completely and just stream the files back through the response stream as they are generated? Take a look at the AbstractExcelView and AbstractPDFView classes of Spring MVC to see some examples of how this is done.

这篇关于从Spring MVC流式传输动态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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