1

I'm trying to read a folder on a network and retrieve a list of txt files. When testing it locally in Eclipse, it works fine, however whenever I deploy it on the Apache Tomcat 7 server it returns null.

It doesn't seem to be an access right problem since the server has access to the folder I'm trying to browse. I'm not sure what is going wrong there, is it a setting on the server I need to change or something else?

private List<File> readDirectory() {
    File test = new File(envMap.get(database));
    List<File> files = new ArrayList<File>();
    try {
        files = FileListing.getFileListing(test);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    List<File> txtFiles = new ArrayList<File>();
    if (files != null) {
        for (File file : files) {
            if (file.isFile() && file.getName().endsWith(".txt")) {
                txtFiles.add(file);
            }
        }
    }
    return txtFiles;
}

I used this http://www.javapractices.com/topic/TopicAction.do?Id=68 for FileListing.getFileListing

After double checking it turns out that I'm getting a FileNotFoundException: Directory does not exist. The directory does exists and the server has access rights on it, so I'm not really sure of what to do.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
noisegrrrl
  • 149
  • 1
  • 13

2 Answers2

0

files cannot be null. Your code uses

static private List<File> getFileListingNoSort(File aStartingDir) throws FileNotFoundException {
    List<File> result = new ArrayList<File>();
    File[] filesAndDirs = aStartingDir.listFiles(); // may return null
    List<File> filesDirs = Arrays.asList(filesAndDirs); // would throw NPE
    for(File file : filesDirs) {
        result.add(file); 
        if (!file.isFile()) {
            //must be a directory               
            List<File> deeperList = getFileListingNoSort(file);
            result.addAll(deeperList);
        }
    }
    return result;
}

Are you sure you are showing us the right code?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

Found out what the problem was, turns out that by default Apache is running under a Local System Account that has no network access. Changing that to another account with network access solved the problem.

Source: http://blog.mattwoodward.com/2010/08/accessing-network-drive-from-apache-and.html

noisegrrrl
  • 149
  • 1
  • 13