如何在Red5中获取应用程序路径(绝对和相对)

发布于 2024-10-10 03:17:15 字数 287 浏览 0 评论 0原文

我制作了一个应用程序并将其作为新文件夹放入 webapps 中,例如 appcustom。
我想阅读一些文本文件。
我如何一般地读取而不是硬编码路径?

目前我正在使用这样的东西:

FileReader fr = new FileReader("webapps/appcustom/<<textfilename>>"); // read a file

如何使其通用?因为明天我可能想将应用程序文件夹名称更改为“custApp”或其他名称。

I made a application and placed it in webapps as new folder, say appcustom.
There are some text files that I want to read.
How do I generically read rather than hardcoding the path?

Currently I am using something like this:

FileReader fr = new FileReader("webapps/appcustom/<<textfilename>>"); // read a file

How do I make it generic? Because tomorrow I may want to change the application folder name to "custApp" or something else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

债姬 2024-10-17 03:17:15

您可以在 Red5 中使用一些系统属性,如果我尝试从通用应用程序类读取资源,我将使用此块:


FileReader fr = new FileReader(String.format("%s/%s/%s", System.getProperty("red5.webapp.root"), appName, fileName));

需要提供应用程序名称(例如:oflaDemo)和您的文件名。

从 ApplicationAdapter 中,您可以访问“可用”应用程序名称:


FileReader fr = new FileReader(String.format("%s/%s/%s", System.getProperty("red5.webapp.root"), getScope().getName(), fileName));

从 Servlet 中,您可以访问完整路径:


FileReader fr = new FileReader(String.format("%s/%s", getServletContext().getRealPath("/"), fileName));

There are a few system properties that you can use in Red5, I would use this block if I were attempting to read a resource from a generic application class:


FileReader fr = new FileReader(String.format("%s/%s/%s", System.getProperty("red5.webapp.root"), appName, fileName));

The application name (ex: oflaDemo) and your file name would need to be supplied.

From an ApplicationAdapter you have access to a "usable" application name:


FileReader fr = new FileReader(String.format("%s/%s/%s", System.getProperty("red5.webapp.root"), getScope().getName(), fileName));

From an Servlet you have access to a full path:


FileReader fr = new FileReader(String.format("%s/%s", getServletContext().getRealPath("/"), fileName));
花落人断肠 2024-10-17 03:17:15
Properties props = new Properties();
   // Looks for the file 'database.properties' in {TOMCAT_HOME}\webapps\{RED5_HOME}\WEB-INF\
   FileInputStream in = new FileInputStream(System.getProperty("red5.config_root") + "/database.properties");
   props.load(in);

我发现这种方式对于阅读任何文件都非常有帮助。

这是完整示例的链接

Properties props = new Properties();
   // Looks for the file 'database.properties' in {TOMCAT_HOME}\webapps\{RED5_HOME}\WEB-INF\
   FileInputStream in = new FileInputStream(System.getProperty("red5.config_root") + "/database.properties");
   props.load(in);

I found this way very helpfull in reading any file.

Here is the link of full example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文