Java Servlets - 写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4378860/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:06:09  来源:igfitidea点击:

Java Servlets - Writing to file

javaservletsfile-io

提问by whirlwin

I'm using the Netbeans IDE, and I'm currently using a GlassFishserver. What I want to do is write to a file.

我使用的是 Netbeans IDE,我目前使用的是GlassFish服务器。我想要做的是写入文件

I looked at some pages, and the code I have now (that is not working as far as I know) looks like:

我查看了一些页面,我现在拥有的代码(据我所知不起作用)看起来像:

 File outputFile = new File(getServletContext().getRealPath("/")
            + "TheFile.txt");
    FileWriter fout = new FileWriter(outputFile);
    fout.write("The Content");
    fout.close();

This is my project's structure:

这是我的项目结构:

alt text

替代文字

Also where will the file get placed?

还有文件会放在哪里?



Edit: I forgot to mention there are some other folders below the ones in the picture: Test Packages, Libraries, Test Libraries and Configuration Files. However I don't think the file would get placed there.

编辑:我忘了提到图片中的文件夹下面还有一些其他文件夹:Test Packages、Libraries、Test Libraries 和 Configuration Files。但是我认为文件不会放在那里。

Edit (newest): I found out the file is stored in the /build/web folder, but this is not appearing in Netbeans. Even after I restarted it.

编辑(最新):我发现该文件存储在 /build/web 文件夹中,但这并没有出现在 Netbeans 中。即使在我重新启动它之后。

采纳答案by BalusC

As you've coded, the file will be placed in public web root. That's where getRealPath("/")will point to. To be precise, it's the folder named Web Pagesas in your screenshot. As an exercise, do the following to figure the absolute path, so that you can find it by OS disk explorer.

正如您编写的那样,该文件将放置在公共 Web 根目录中。那就是getRealPath("/")将指向的地方。准确地说,它是屏幕截图中名为Web Pages的文件夹。作为练习,请执行以下操作来计算绝对路径,以便您可以通过 OS 磁盘资源管理器找到它。

System.out.println(file.getAbsolutePath());

I don't do Netbeans, but likely you need to refresh the folder in your IDE after the write of the file so that it appears in the listing in the IDE. Click the folder and press F5. This is at least true for Eclipse.

我不使用 Netbeans,但您可能需要在写入文件后刷新 IDE 中的文件夹,以便它出现在 IDE 的列表中。单击文件夹并按 F5。至少对于 Eclipse 来说是这样。

That said, this approach is not recommended. This won't work when the servletcontainer isn't configured to expand the WAR on disk. Even when it did, you will lose all new files and changes in existing files when the WAR is been redeployed. It should not be used as a permanent storage. Rather store it on a fixed path outside the webapp or in a database (which is preferred since you seem want to reinvent a CMS).

也就是说,不推荐这种方法。当 servletcontainer 未配置为在磁盘上扩展 WAR 时,这将不起作用。即使这样做了,当重新部署 WAR 时,您也会丢失所有新文件和现有文件中的更改。它不应用作永久存储。而是将其存储在 web 应用程序之外的固定路径或数据库中(这是首选,因为您似乎想重新发明一个 CMS)。

回答by Thorbj?rn Ravn Andersen

Note that this is in no way guaranteed to work in all web containers or through restarts and will most likely be overwritten by a redeployment.

请注意,这绝不能保证在所有 Web 容器中或通过重新启动都能工作,并且很可能会被重新部署覆盖。

If you want to be able to allow your user to update content, you need to store the new content somewhere and have a servlet or a JSP-page or a facelet retrieve the new content from the backing storage and send it to the browser.

如果您希望能够允许您的用户更新内容,您需要将新内容存储在某处,并让 servlet 或 JSP 页面或 facelet 从后备存储中检索新内容并将其发送到浏览器。

回答by A_M

See the documentation for getRealPath. It returns you the location on the disk for something specified with a URL.

请参阅getRealPath的文档。它为您返回磁盘上的位置,用于使用 URL 指定的内容。

I'm guessing your file is in the root of your web application on the disk within Glassfish (where the WAR file is extracted). I don't know enough about Glassfish to say where that will be.

我猜您的文件位于 Glassfish 磁盘上的 Web 应用程序的根目录中(提取 WAR 文件的位置)。我对 Glassfish 的了解还不够多,无法说明它会在哪里。

Also, note you are using string concatenation to create the file name, so if the getRealPath call doesn't return a String with a "/" on the end, then you might be creating a file in the parent directory of your web app. Perhaps best to use a File object for the parent directory when creating the File object for the actual file. Check out the File API.

另请注意,您正在使用字符串连接来创建文件名,因此如果 getRealPath 调用未返回末尾带有“/”的字符串,则您可能正在 Web 应用程序的父目录中创建一个文件。在为实际文件创建 File 对象时,最好将 File 对象用于父目录。查看文件 API。

I'd recommend creating the file outside of your web app. If you redeploy your WAR file then you might delete your file, which probably isn't what you want.

我建议在您的网络应用程序之外创建文件。如果您重新部署 WAR 文件,那么您可能会删除您的文件,这可能不是您想要的。

Being in a servlet makes little difference to the fact that you want to output a file. Just follow the standard file APIs as a starting point. Here'sa tutorial.

在 servlet 中与您想要输出文件的事实几乎没有区别。只需遵循标准文件 API 作为起点即可。这是一个教程。