Search code examples
javaspringfile-iorssrss2

Can I save a file on server using FileOutputStream?


I am trying to create an RSS feed dynamically and store it on server, such that to access the created RSS feed I can just type in the URL from the browser. Is it possible to do so?

I can create the RSS feed, but when I try to save it on server using FileOutputStream("WebContent/filename.xml") it gives me FileNotFoundException. Is my approach correct, or is there any other way to store a file on the server and access it using a simple URL?

I am using spring schedulers to run my application every minute to create the RSS feed dynamically.


Solution

  • Generally, you don't "generate & store" such things. You serve dynamically & cache them (both http cache (for clients) and internal cache (so that you don't fetch multiple times)). Spring 3.1 cache abstraction would allow that easily.

    In order to store files to the server you need to know what the root directory of the webapp is. You can get it by request.getServletContext().getRealPath("/"). Note that these files will be gone after undeploy, so you might consider storing them in an absolute, outside-app location. Also note that you can't write them in unexploded war archives. And another thing - there is no WebContent by default - it is the project directly but it does not correspond to a directory in the web app distributable.