Java 如何获取项目的根路径?

Java 如何获取项目的根路径?,java,jsp,Java,Jsp,我有在Eclipse中开发的Tomcat中运行的JSP项目 我想在项目中存储一些文件 以下是我的项目结构: .settings build data ImportedClasses src WebContent .classpath .project 我想从位于WebContent中的JSP文件中的代码访问数据文件夹 尝试了以下一些代码: File userDataDirFile = new File ( "data" ); String path = userDataDirFile.getA

我有在Eclipse中开发的Tomcat中运行的JSP项目

我想在项目中存储一些文件

以下是我的项目结构:

.settings
build
data
ImportedClasses
src
WebContent
.classpath
.project
我想从位于WebContent中的JSP文件中的代码访问数据文件夹

尝试了以下一些代码:

File userDataDirFile = new File ( "data" );
String path = userDataDirFile.getAbsolutePath();
印刷品

C:\Program Files\eclipse\data\users
C:/Workspaces/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/survey/WEB-INF/classes/
C:\Program Files\eclipse
然后

印刷品

C:\Program Files\eclipse\data\users
C:/Workspaces/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/survey/WEB-INF/classes/
C:\Program Files\eclipse
另一个:

System.getProperty("user.dir")
印刷品

C:\Program Files\eclipse\data\users
C:/Workspaces/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/survey/WEB-INF/classes/
C:\Program Files\eclipse
我认为第二个解决方案应该可以工作,但它无法将我定位到项目的根文件夹。任何人都可以建议?

编辑:不在WebContent中的文件将不会与您的war一起部署。您必须将代码中使用的文件放在WebContent中,并尝试将其指向web应用程序的根文件夹: 如果您的文件与WEB-INF位于同一文件夹中,则:

ServletContext context = getContext();
String fullPath = context.getRealPath("/data");
顺便说一下,如果您不想直接访问数据文件,建议将其放入WEB-INF中,这样就没有人可以直接访问它们

String caminhoProjeto = Thread.currentThread().getContextClassLoader().getResource("").getPath();
这类似于src路径,但却是部署和运行时后二进制文件上下文的类路径

例如,我的类必须使用这种方式,即项目的根目录:

PropertiesReader.java:

public class PropertiesReader {

    public static String projectPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    public static String propertiesPath = "/META-INF/";

    public static Properties loadProperties(String propertiesFileName) throws IOException {
        Properties p = new Properties();
        p.load(new FileInputStream(projectPath + propertiesPath + propertiesFileName));
        return p;
    }

    public static String getText(String propertiesFileName, String propertie) {

        try {
            return loadProperties(propertiesFileName).getProperty(propertie);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return propertie;
    }
}
PersonRegistration.java:

@ManagedBean
public class PersonRegistration {
    private Integer majority = Integer.parseInt(PropertiesLeitor.getText("Brasil.properties", "pessoa.maioridade"));
}
src/META-INF中的Brasil.properties:

pessoa.maioridade = 18

为什么要访问数据文件夹?当应用程序可能打包在war中并部署到某个服务器上时,情况如何?在这些情况下,数据文件夹可能位于不同的位置或war中,因此最好在一些配置/属性存储文件中声明它,不在WebContent中的数据库等文件将不会随您的war检查我的更新答案一起部署。当我将数据文件夹打包为war文件并部署到云时,该文件夹是否会更改位置?数据文件夹由一些文件组成,例如本地化文件、用户文件、格式文件和其他一些文件。因为这个项目是一个开源项目,而且必须很快启动,所以我不想在那里更改很多代码。因此,我认为最好的方法是获取根项目并引用数据文件夹。原始代码需要C文件夹根中的数据文件夹。好的,我理解。顺便说一句,如何拥有getContext?它给了我一个未定义的错误。是getServletContext吗?我尝试使用getServletContext,它返回我C:\Workspaces\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\survey\data:从您的servlet是的,它是getServletContext哦,它不应该在JSP文件中?抱歉,我不是JSP方面的专家。是的,这是正常的,这是预期的结果,因为从Eclipse启动Tomcat应用程序将始终部署在这样一个文件夹中,tmp0可能会更改为tmp1 tmp2。这里的文件是原始文件的副本。