java 我可以使用 FileOutputStream 在服务器上保存文件吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6669014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I save a file on server using FileOutputStream?
提问by Ritesh Mengji
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?
我正在尝试动态创建 RSS 提要并将其存储在服务器上,以便访问创建的 RSS 提要,我只需从浏览器中输入 URL。有可能这样做吗?
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?
我可以创建 RSS 提要,但是当我尝试将其保存在服务器上时,使用FileOutputStream("WebContent/filename.xml")
它会给我FileNotFoundException
. 我的方法是否正确,或者有没有其他方法可以将文件存储在服务器上并使用简单的 URL 访问它?
I am using spring schedulers to run my application every minute to create the RSS feed dynamically.
我正在使用 spring 调度程序每分钟运行我的应用程序以动态创建 RSS 提要。
回答by Bozho
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.
通常,您不会“生成和存储”这些东西。您动态地提供服务并缓存它们(http 缓存(用于客户端)和内部缓存(这样您就不会多次获取)。Spring 3.1 缓存抽象可以轻松实现这一点。
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.
为了将文件存储到服务器,您需要知道 webapp 的根目录是什么。你可以通过request.getServletContext().getRealPath("/")
. 请注意,这些文件将在取消部署后消失,因此您可以考虑将它们存储在应用程序外部的绝对位置。另请注意,您不能将它们写入未爆炸的War档案中。另一件事 -WebContent
默认情况下没有- 它直接是项目,但它不对应于可分发的网络应用程序中的目录。
回答by JustinKSU
My recommendation would be to expose a directory on the server using your HTTP daemon (probably Apache) and then have the application write there. As far as I know, you can't write files inside your WAR/EAR.
我的建议是使用您的 HTTP 守护程序(可能是 Apache)在服务器上公开一个目录,然后让应用程序在那里写入。据我所知,您不能在 WAR/EAR 中写入文件。
回答by JB Nizet
You're confusing the directory that you're using to store the source files of your project and the directory where the application is deployed on the server.
您混淆了用于存储项目源文件的目录和应用程序在服务器上部署的目录。
First define how and where you will deploy the application (if it's as a war file, you won't even have a root directory), and then design your application accordingly.
首先定义部署应用程序的方式和位置(如果它是一个war 文件,您甚至没有根目录),然后相应地设计您的应用程序。
If you want to stay portable, you should define a configuration parameter (in your web.xml) defining where external files are stored, and have a servlet reading files from this directory and writing their contents in the response output stream.
如果您想保持可移植性,您应该定义一个配置参数(在您的 web.xml 中)定义外部文件的存储位置,并让一个 servlet 从该目录读取文件并将其内容写入响应输出流中。
But you could also configure your server to serve static files from a specific directory and write the XML file to this directory. Read the documentation of your server to know if and how it's possible to do that.
但您也可以将服务器配置为从特定目录提供静态文件,并将 XML 文件写入该目录。阅读您的服务器的文档以了解是否以及如何做到这一点。
回答by timaschew
Here is my solution in a Servlet method doGet(...). It don't save the RSS in the file system, it will be created every time (when there is a request). I write it out as the HTTP response.
这是我在 Servlet 方法 doGet(...) 中的解决方案。它不会将 RSS 保存在文件系统中,它会在每次(有请求时)创建。我把它写成 HTTP 响应。
response.setContentType("application/rss+xml");
response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expires", 0); // prevents caching at the proxy server
PrintWriter out = response.getWriter();
out.print("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
[...]
out.print("</xml>");
out.flush();
out.close();
// set header information
When its necessary to write it on the filesystem, here is a solution, I used it to save an image on the server, its again in a servlet method:
当需要将它写入文件系统时,这里有一个解决方案,我用它在服务器上保存图像,它再次在 servlet 方法中:
String absoluteFilesystemPath = getServletContext().getRealPath("/");
System.out.println(absoluteFilesystemPath);
File f = new File(absoluteFilesystemPath+"file.xml")
回答by Reverend Gonzo
What Justin says is right, but I'd go further and say do you actually need to store it on disk?
贾斯汀说的是对的,但我更进一步说你真的需要将它存储在磁盘上吗?
If it's not that big of a file, (my bet would be not), then just have the url point to a servlet which returns it from cached memory, and then have that servlet also rebuilds it every minute.
如果它不是那么大的文件,(我敢打赌不会),那么只需让 url 指向从缓存内存中返回它的 servlet,然后让该 servlet 也每分钟重建一次。