1

我正在为我部署的每个应用程序在内部服务器上的受保护文件夹中维护包含数据库凭据的属性文件。我不允许将凭据存储在 WAR 文件中。

在我的 PC 上测试时,路径是 windows mount,但是当我部署到服务器时,它是 unix 路径

我一直在处理这样的检索

//siteDbCedentialFolder obtained from web.xml

Properties prop = new Properties();
InputStream in = null;
try {
    //assume running on server first
      in = new FileInputStream("/abc/data/" + siteDbCedentialFolder + "/props.txt");
    } catch (java.io.IOException ex) {
    // Probabaly Running locally
      in = new FileInputStream("W:/internal/abc/data/" + siteDbCedentialFolder + "/props.txt");
    } catch (Exception xx) {
    xx.printStackTrace();
    }
prop.load(in);
in.close();

我的方法是使用 catch 来获取本地路径,还是有更好的方法来编码?

4

1 回答 1

1

Better way is to put the txt file in user home directory and get it by

System.getProperty("user.home");

and to get the full path

String pathToFile = System.getProperty("user.home") + File.separator + "data" + File.separator + "props.txt";

It would work across all the platform, provided that you need to put the file at proper place (user home)

于 2012-06-27T12:57:13.663 回答