【问题标题】:Relative File Path references in Apache Tomcat application [duplicate]Apache Tomcat 应用程序中的相对文件路径引用 [重复]
【发布时间】:2015-05-09 09:47:05
【问题描述】:

我目前正在开发一个 Web 应用程序,我有一个名为 File.xml 的 XML 文件,其中包含 PostgreSQL 连接参数:登录名、密码、ip_address、pilote、端口、bdd。 .我将此文件添加到 Apache Tomcat 的 bin 文件夹中。我有 class.jav,它使用 XML 文件的参数连接到数据库。

当我运行课程时,我得到else 行的结果(找不到文件)。我怀疑该类不会访问 apache bin 文件夹下的 File.xml。怎么办?

    private ParamBDD()
{
    SAXBuilder sxb = new SAXBuilder();
    try
    {
        mes_documents mes=new mes_documents();
        Fichier fichier = new Fichier();
        File file=new File("File.xml");
        //File file=new File(this.getClass().getResource("/resources/Fichiers_parametres/parametreconnexion_crypte.xml").getFile());



            String str_fichier ="File.xml";


            if (file.isFile())
            {
                org.jdom.Document document = sxb.build(new File(str_fichier));

                Element racine = document.getRootElement();
                List listParam = racine.getChildren("param");

                Iterator i = listParam.iterator();
                while (i.hasNext())
                {
                    Element courant = (Element) i.next();

                    pilote = courant.getChild("pilote").getText().trim();
                    utilisateur = courant.getChild("login").getText().trim();
                    password = courant.getChild("password").getText().trim();
                    adresseIP = courant.getChild("adresseIP").getText().trim();
                    port = courant.getChild("port").getText().trim();
                    BDDGenerale = courant.getChild("bdd").getText().trim();
                    System.out.println("BDD Generale:"+BDDGenerale);

                    //JOptionPane.showMessageDialog(null, "Fin du fichier","",JOptionPane.INFORMATION_MESSAGE);
                }

            }
            else JOptionPane.showMessageDialog(null, "le fichier contenant les parametres n'existe pas","",JOptionPane.INFORMATION_MESSAGE);

    }

【问题讨论】:

  • 请将您的问题翻译成英文。
  • 嗨,阿斯玛,欢迎来到 StackOverflow。你可以编辑你的帖子并提出你的问题in English吗?
  • 我目前正在开发一个 Web 应用程序,我有一个名为 File.xml 的 xml 文件,其中包含与 postgresql 数据库的连接参数。我将此文件添加到 apache 的 bin 文件夹中。我有 class.jav,它使用 xml 文件的参数连接到数据库。当我运行课程时,我得到了 else 行的结果。我怀疑该类不会访问 apache bin 文件夹下的 File.xml。怎么办?
  • 刚刚翻译。阿斯玛,检查你的牙套。
  • 如果您的File file = ... 行确实是注释掉的行,那么这将是您真正的问题。如果File file = .... 行是您当前拥有的= new File("File.xml"); 行,那么您的问题是因为您当前目录中没有名为File.xml 的文件。是哪个file

标签: java tomcat jakarta-ee


【解决方案1】:

Java 中的所有相对文件路径都是相对于用户启动 Java 程序时所在的目录进行解析的。系统属性中记录了你启动程序时所在的目录:System.getProperty("user.dir");

因此,例如,如果您在启动 tomcat 时位于 /home/asma/myproject,那么 user.dir 文件夹将为 /home/asma/myproject

然后,当您有文件引用时,例如:

File file = new File("File.xml");

然后Java系统会寻找文件/home/asma/myproject/File.xml

您需要做的是使文件引用绝对路径,或者确保您在实际启动tomcat时位于bin文件夹中......(这很痛苦......所以不要这样做)。

将您的代码更改为:

File file = new File(System.getProperty("catalina.base") + "/bin/File.xml");

【讨论】:

    猜你喜欢
    • 2016-05-13
    • 2010-09-10
    • 2013-12-20
    • 2011-01-24
    • 2013-10-25
    • 2012-05-18
    • 2011-12-18
    • 2012-03-02
    • 1970-01-01
    相关资源
    最近更新 更多