简体   繁体   中英

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

I'm struggling to get my simple Tomcat app to work.

I have csv file placed in 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

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:

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.

Try using 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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