My Controller method (which has a method to list the files, from a path mentioned in the property file, on JSP)
private String[] getFileListing(String servers) throws IOException {
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream input = getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(input);
if (servers.equals("MS1")) {
File f = new File(prop.getProperty("path.MS1"));
String[] list = f.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt")||name.endsWith(".log");
}
});
return list;
} else {
File f = new File(prop.getProperty("path.MS2"));
String[] list = f.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt")||name.endsWith(".log");
}
});
return list;
}
}
I want to display the log files with common extensions like- .txt or .log But the system creates files with .1 .2 .3 extensions also.
Thanks!
You could do a couple of things with this code, i'd honestly just refactor this quite a bit and have your whitelist be an ArrayList of extensions.
For example:
private String[] getFileListing(String servers) throws IOException {
List<String> allowedExtensions = Arrays.asList("log txt".split(" "));
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream input = getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(input);
File f = new File(prop.getProperty("path." + servers));
String[] list = f.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return allowedExtensions.contains(name.substring(name.lastIndexOf(".") + 1, name.length()));
}
});
return list;
}
But, that being said, there are a couple of other things in your code that could be optimized, for example, as i changed above, you're checking if a servers string equals something, then loading it, i assumed because you only have MS1 and MS2, that being said, if the intention was that it loads MS2 always on every servers parameter except for MS1, you could do that like so:
private String[] getFileListing(String servers) throws IOException {
List<String> allowedExtensions = Arrays.asList("log txt".split(" "));
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream input = getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(input);
File f = new File(prop.getProperty("path.ms2"));
if(servers.equals("MS1")){
f = new File(prop.getProperty("path." + servers));
}
String[] list = f.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return allowedExtensions.contains(name.substring(name.lastIndexOf(".") + 1, name.length()));
}
});
return list;
}
If you want to support other things, like .log.1, or .log.2, this approach wouldn't work, but you could invert it and make the list a blacklist of extensions, or use regex to match filenames, for example, for any file ending in .log or .log.
this regex will match any filename following what appear to be your whitelist conditions:
^.+(.log|.txt)(.\d)?$
You can see this with some exmple matches here