我正在使用 commons-io jar 监视目录中的文件创建和修改。我能够在 Eclipse 控制台中获得结果。
final long pollingInterval = 5 * 1000;
String FOLDER = "C:/test";
File folder = new File(FOLDER);
folder.setReadable(true);
if (!folder.exists()) {
// Test to see if monitored folder exists
throw new RuntimeException("Directory not found: " + FOLDER);
}
FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor =
new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
// Is triggered when a file is created in the monitored folder
@Override
public void onFileCreate(File file) {
try {
// "file" is the reference to the newly created file
System.out.println("File created: "
+ file.getCanonicalPath());
getNewMethod(file);// here in this method i am not able to return since its void.
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
@Override
public void onFileChange(File file) {
try {
System.out.println("File modified: "
+ file.getCanonicalPath());
getNewMethod(file); // here in this method i am not able to return since its void.
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();
问题是当它调用 onFileCreate 和 onFileChange 方法时我无法返回文件名。如何实现这一点?而且我还试图在 onFileCreate 和 onFileChange 中调用一种方法,它返回一个列表。如何返回列表?因为在这个监听器中我没有看到除了 void 的返回参数。
//调用新方法
public String getNewMethod(File newfile) throws IOException{
System.out.println("getList method called : "+newfile.getCanonicalPath());
return "redirect:finalPage"; // here the redirection is not happening
}
当我看到文件更改/创建事件被触发时,我需要在 jsp 中更新更改。如何实现这一点?