views:

356

answers:

3

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.

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.

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?

Thanks!

A: 

Ideal for this is using an lightweight proxying server in front of your appserver, like a nginx or lighthttpd. You can configure it for serving static content, without calling your app.

If directory and files so dynamic, you can prepare real path to file at your controller and give this filepath to the frontend server, using headers. For example for nginx it's a X-Accel-Redirect header. Read more about this (and follow links for other http servers) there

splix
That's certainly an option, however, like the tomcat option by BalusC, I think this would be difficult to integrate in a way that easily works for dev environments as well as on the server.
zpinter
For dev environment you can use byte-to-byte output, you can easily create an controller for this. But when deploy on the production server, you can configure this controller for output with only headers.
splix
A: 

or the standard application server spec?

Yes, there is. As you didn't mention which one you're using, I'll give a Tomcat-targeted answer. All you basically need to do is to add a Context element for /path/to/your/resources in /conf/server.xml:

<Context docBase="/path/to/your/resources" path="/resources" />

This way they'll be accessible through http://example.com/resources/...

BalusC
This might work... however, I think it'd be hard to integrate the location of the directory in my spring config so that it works via deploy, build scripts, and local developer machines via Eclipse.
zpinter
A: 

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.

matt b
Thanks. The generation takes too long (approximately 10 seconds) to always generate it live. However, I might be able to derive my own AbstractStreamingFileView from these sources.
zpinter