简体   繁体   English

无法从 Tomcat 服务器访问 Java 资源文件夹中的文件

[英]Can't access file in Java resources folder from Tomcat server

I'm struggling to get my simple Tomcat app to work.我正在努力让我的简单 Tomcat 应用程序正常工作。

I have csv file placed in src/main/resources/cities.csv我有 csv 文件放在 src/main/resources/cities.csv

and in my test main method everything goes well.在我的测试主要方法中,一切顺利。 However when I refer to this using a method in servlet I get: java.nio.file.NoSuchFileException但是,当我使用 servlet 中的方法引用它时,我得到:java.nio.file.NoSuchFileException

FileOperations:文件操作:

public class FileOparations {

static private final Path path = Paths.get("src/main/resources/cities.csv");

public static List<City> getCitiesList() {
    return getCitiesStream().collect(Collectors.toList());
}

public static Stream<City> getCitiesStream() {
    try {
        return Files.readAllLines(path)
                .stream()
                .skip(1)
                .map((String line) -> {
                    return line.split("\",\"");
                })
                .map((String[] line) -> {

                    String[] newData = new String[line.length];
                    for (int i = 0; i < line.length; i++) {
                        newData[i] = line[i].replaceAll("\"", "");
                    }
                    return newData;
                })
                .map((String[] data) -> {
                    String name = data[0];
                    String nameAscii = data[1];
                    String gps = data[2] + ":" + data[3];
                    String country = data[4];
                    String adminName = data[7];
                    String capitol = data[8];
                    long population;
                    try {
                        population = Long.parseLong(data[9]);
                    } catch (NumberFormatException e) {
                        population = Integer.MIN_VALUE;
                    }
                    int id = Integer.parseInt(data[10]);
                    return new City(name, nameAscii, id, country, gps, capitol, population, adminName);
                });
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Stream.empty();
}

My code in servlet looks like this:我在 servlet 中的代码如下所示:

protected void doGet(HttpServletRequest request, HttpServletResponse response){

    List<City> cities = FileOparations.getCitiesList();
    request.setAttribute("cities", cities);
    request.getRequestDispatcher("result.jsp").forward(request, response);
}

I'm surprised, because I'm not passing any URL through servlet, I want to call static method from Java. Method getCitiesList calls stream, mapping and returns ready to use list.我很惊讶,因为我没有通过 servlet 传递任何 URL,我想从 Java 调用 static 方法。方法 getCitiesList 调用 stream,映射并返回准备使用列表。

Try using getServletContext() :尝试使用getServletContext()

String relativePath = "/resources/cities.csv";
InputStream input = getServletContext().getResourceAsStream(relativeWebPath);

Relative path will be the path from the directory, expanded from the WAR file in your tomcat. So before building, it should be in src/main/webapp/相对路径将是目录的路径,从您的 tomcat 中的 WAR 文件展开。因此在构建之前,它应该在 src/main/webapp/

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

相关问题 无法访问Java中的资源文件夹 - Can't access resources folder in Java 无法访问资源文件夹中的文件 - Can't Access File in Resources Folder 从 java 中的资源文件夹访问文件(运行 JavaScript 文件) - Access a file from a resources folder in java (Run a JavaScript file) 为什么 java 在我的资源文件夹中找不到文件? - Why java can't find the file in my resources folder? 无法使用资源文件夹中的文件进行jUnit测试 - Can't jUnit test with file from resources folder Java 无法访问 src/main/resources 中的 txt 文件 - Java can't access txt file in src/main/resources 无法将 web 项目添加到 tomcat 服务器“没有可以从服务器添加或删除的资源” - Java 17 - Not able to add web project to tomcat server "There are no resources that can be added or removed from the server" - Java 17 如何最好地从资源文件夹访问文件 - How best to access a file from resources folder 无法在Java Spring Boot中将文件上传到tomcat托管服务器,始终在fedora中显示对给定文件上传路径的访问被拒绝 - Can't upload file in java spring boot to the tomcat hosting server, always show access denined for the given file upload path in fedora 访问资源文件夹中的文件 - Access file in Resources folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM