简体   繁体   English

应用程序根目录中目录的相对路径-Java Web应用程序

[英]Relative path to a directory in application root - Java Web Application

I am struggling with getting a relative path to the fileroot directory that I have placed in the web root folder. 我正在努力获取放置在Web根文件夹中的fileroot目录的相对路径。 I am trying to access this directory from a class to return the names of files in it via a Web Service . 我试图从一个类访问此目录,以通过Web Service返回其中的文件名。 The folder contains images and XML files. 该文件夹包含图像和XML文件。

Here is my project structure as seen from exploded .WAR file - 这是我从分解的.WAR文件中看到的项目结构-

MyProject 我的项目
- fileroot -文件根
- css -CSS
- ... -...
- WEB-INF -网络信息
|--classes | --classes

I am able to get this to work when I use a hard-coded absolute path on my local drive to read files on the directory. 当我在本地驱动器上使用硬编码的绝对路径读取目录中的文件时,我能够使它工作。 However, I need this to work on the web server where absolute path's are not possible. 但是,我需要它在无法使用绝对路径的Web服务器上工作。

I need to access this folder from within my class to be able to apply some processing on the XML while being able to serve images from the folder for webpages. 我需要从班级内部访问此文件夹,以便能够在XML上进行一些处理,同时能够从网页的文件夹中提供图像。

Here is the code written for reading the directory - 这是为读取目录而编写的代码-

package com.xyz.executor;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

/**
 *
 * @author AJ
 */
public class GetDirectories {

    public String basePath = "/fileroot";
    public String directoryName;

    public GetDirectories() {
    }

    public GetDirectories(String DirectoryName) {
        this.directoryName = DirectoryName;

        basePath = basePath + "/" + directoryName;
    }

    public ArrayList<String> read() {
        ArrayList<String> DirectoryList = new ArrayList<>();

        try {
            File f = new File(basePath); // current directory

            FileFilter directoryFilter = new FileFilter() {
                public boolean accept(File file) {
                    return file.isDirectory();
                }
            };

            File[] files = f.listFiles(directoryFilter);
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.print("directory:");
                    DirectoryList.add(file.getName());
                } else {
                    System.out.print("     file:");
                }
                System.out.println(file.getName());
            }
        } catch (Exception e) {
            System.out.println("Exception occurred (GetDirectories) : " + e.toString());
            e.printStackTrace();
            System.out.println("**************************");
        }
        return DirectoryList;
    }
}

I get a null pointer exception as f.listFiles(directoryFilter) returns null because of which I suspect there is an issue with the path. 我收到一个null pointer exception因为f.listFiles(directoryFilter)返回null ,因此我怀疑路径存在问题。

Question 1 , How can I get the application root path within a class file? 问题1 ,如何在类文件中获取应用程序的根路径?
Question 2 , Am I doing the right thing by placing the directory within the application root? 问题2 ,通过将目录放在应用程序根目录中,我做对了吗? Or should I place this in another location on the drive? 还是应该将其放置在驱动器上的其他位置?

PS: I wrote a web application earlier which read a .properties file stored in the classpath similar to this question . PS:我之前编写了一个Web应用程序,该应用程序读取类似于此问题的类路径中存储的.properties文件。 However, this does not seem like a good idea for bulk number of files 但是,对于大量文件来说,这似乎不是一个好主意

On an application server, the default path is either the path from which the server was started (ie the folder you were in when you launched the server) or the bin folder. 在应用程序服务器上,默认路径是启动服务器的路径(即,启动服务器时所在的文件夹)或bin文件夹。 So the directory and the files should be in it. 因此,目录和文件应位于其中。 A quick way to check what path you're in is printing or logging the result of f.getAbsolutePath() , this will tell you what folder the server is defaulting to. 一种检查您所在路径的快速方法是打印或记录f.getAbsolutePath()的结果,这将告诉您服务器默认使用的文件夹。

You're opening the file normally ( new File(...) ) and not as a resource and therefore the resource path or class path are not used. 您正在正常打开文件( new File(...) ),而不是将其作为资源打开,因此未使用资源路径或类路径。

When dealing with application servers it is best to avoid using the default path as it changes easily. 处理应用程序服务器时,最好避免使用默认路径,因为它很容易更改。 Either have a system property or equivalent that defines where the files will be searched for or, if you're shipping the file you want to read together with the application, put it inside the WAR and open it as a resource: 具有定义要在何处搜索文件的系统属性或等效项,或者,如果要与应用程序一起运送要读取的文件,请将其放在WAR中并作为资源打开:

YourClass.class.getResourceAsStream("/...");

or 要么

YourClass.class.getResource("/...");

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

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