简体   繁体   English

Java从Web目录读取属性文件

[英]Java reading properties file from web directory

I'm trying to read a config.properties file from my JARs local directory where it's run from a website. 我正在尝试从我的JAR本地目录中读取config.properties文件,该文件是从网站上运行的。 I have tried a bunch of different code from Streaming it to getting the class path, etc. Finally the one I'm trying now is the following code: 我尝试了很多不同的代码,从流式传输到获取类路径等。最后,我现在尝试的是以下代码:

        FileInputStream f = new FileInputStream("./config.properties");
        if(f != null) {
            prop.load(f);
            db = prop.getProperty("database");
            dbuser = prop.getProperty("dbuser");
            dbpass = prop.getProperty("dbpass");
            host = prop.getProperty("host");
            iloc = prop.getProperty("iloc");
        }

This evaluates perfectly while testing and running from inside Netbeans, but the second I place it on the web and run it, it no longer finds the config file even though it's the same file in the same directory. 从Netbeans进行测试和运行时,此方法的评估效果理想,但是第二次将其放置在Web上并运行它,即使它在同一目录中是同一文件,也不再能找到配置文件。 I've tried with the ./ and without, still same result. 我尝试了./ ,但没有,结果还是一样。

you have to have some absolutes or some run time params. 您必须具有一些绝对值或一些运行时参数。 What we do is have an app home as a system property. 我们要做的是拥有一个应用程序主页作为系统属性。 thus when starting java on windows its /app1 but on a server its /etc/app1 因此,当在Windows的/ app1上启动Java但在服务器的/ etc / app1上启动java

Using the -D when launching java or javaw 启动java或javaw时使用-D

java -Dapp.home=/etc/app1 -Dapp.other=somevalue my.package.AppMain

Then in our app we have a FileUtils class that reads this in static init and has a static getter for it. 然后在我们的应用程序中,我们有一个FileUtils类,该类以静态init读取它,并具有一个静态getter。

Sample 样品

public class FileUtils{
    private static File appHome = null;
    static{
        String home = System.getProperty("app.home");//-D that you specify or some standard that java has see javadoc
        if(home == null){
            //logger as needed
            home = "/app1";

        }
        appHome = new File(home);
    }


    public static File getAppHome(){
        return appHome;
        }


    ...

a website - running java as the web server/ app server has a file system too. 一个网站-作为Web服务器/应用程序服务器运行Java的文件系统也是如此。 if you do not have access to it then you cant edit the config also right? 如果您无权访问它,那么您也无法编辑配置,对吗? if you know the path to the config but cannot change the java command you could do something like this: 如果您知道配置的路径,但是无法更改java命令,则可以执行以下操作:

Suggestion 1 on your dev system make an arbitary file like /dev/randome4343433Name.txt if this file exists you know yur in dev and use that path else go for the path in the server. 在您的开发系统上的建议1制作一个类似于/dev/randome4343433Name.txt的文件,如果该文件存在,则说明您在dev中是已知的,并使用该路径作为服务器中的路径。

Suggestion 2 Or use the path on the server. 建议2或使用服务器上的路径。 Even if the server is linux and dev is windows you can do this just use this slash / and make the same file system in the dev drive but do not mention the drive letter in code. 即使服务器是linux并且dev是Windows,您也可以使用此斜杠/并在dev驱动器中创建相同的文件系统,但不要在代码中提及驱动器号。 so if on server its at /etc/app1/config.txt then on local in drive you start make a etc\\app1 folder and put config there and uin code use "/etc/app1/config.txt" 因此,如果在服务器上位于/etc/app1/config.txt,然后在驱动器本地上,则开始创建一个etc \\ app1文件夹,并将配置放在此处,并且uin代码使用“ /etc/app1/config.txt”

For java.applet.Applet or JApplet use Gets the URL of the document in which this applet is embedded. 对于java.applet.Applet或JApplet,使用获取嵌入该applet的文档的URL。 then make a new URL adding the path to config.txt, open the input stream on URL and you can read the file on the client. 然后创建一个新的URL,将路径添加到config.txt,打开URL上的输入流,即可在客户端上读取文件。

remember everything you do is on client, so you cannot write back unless you have a jsp page on server and connect to that using some security (password) from the applet 记住您所做的一切都是在客户端上完成的,因此除非您在服务器上有一个jsp页面并使用小程序中的一些安全性(密码)连接到该页面,否则您将无法回写

In commons-configurations jar there is this class: 在commons-configurations jar中有此类:

PropertiesConfiguration cfg = new PropertiesConfiguration("filePath"); PropertiesConfiguration cfg = new PropertiesConfiguration(“ filePath”);

Just check it 只是检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM