【问题标题】:File Montoring | Not updating in jsp while listening文件监控 |收听时不更新jsp
【发布时间】:2014-12-09 06:30:28
【问题描述】:

我正在使用 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 中更新更改。如何实现这一点?

【问题讨论】:

    标签: java file spring-mvc apache-commons-io file-monitoring


    【解决方案1】:

    您可以在创建监听器之前创建 File 对象并在 onFileCreate() 方法中设置文件。您还可以使用 OuterClass.this.OuterClassMethod() 调用匿名类方法中的方法。

    File file;
    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
    this.file = file;                
    System.out.println("File created: "
                            + file.getCanonicalPath());
    OuterClassName.this.newMethod();
    
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
    
            }
            @Override
            public void onFileChange(File file) {
                try {
    
                    System.out.println("File modified: "
                            + file.getCanonicalPath());
                } catch (IOException e) {
                    e.printStackTrace(System.err);
                }
    
            }
    
        };
    public list newMethod(){
    // returns list here
    List list = new ArrayList();
    return list;
    }
    

    注意:将 OuterClassName 替换为实际的外部类名称。

    【讨论】:

    • 感谢您的回复..在这里调用该方法不是问题..当我触发文件更改事件时,我需要将更改返回给 spring 控制器。它没有被退回..当我只重新加载页面时,它被退回..我该怎么办??
    【解决方案2】:
    import java.io.File;
    
    
    public class Test {
    
        public void testMonitoring {
    
            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
    
               private String filePath;
    
               @Override
               public void onFileCreate(File file) {
                   try {
                       // "file" is the reference to the newly created file
                       System.out.println("File created: "
                               + file.getCanonicalPath());
    
                       setFilePath(file.getCanonicalPath());
    
    
                   } catch (IOException e) {
                       e.printStackTrace(System.err);
                   }
    
               }
               @Override
               public void onFileChange(File file) {
                   try {
    
                       System.out.println("File modified: "
                               + file.getCanonicalPath());
                   } catch (IOException e) {
                       e.printStackTrace(System.err);
                   }
    
               }
            /**
             * @return the filePath
             */
            public String getFilePath() {
                return filePath;
            }
            /**
             * @param filePath the filePath to set
             */
            public void setFilePath(String filePath) {
                this.filePath = filePath;
            }
    
           };
           observer.addListener(listener);
    
           //getting the file path
           System.out.println(listener.getFilePath());
    
           monitor.addObserver(observer);
           monitor.start();
        }
    
    }
    

    【讨论】:

    • 嗨..@user1898711,仔细看。它在那里:)
    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 2018-01-20
    • 2012-03-26
    相关资源
    最近更新 更多