java 如何在 spring 中监视文件夹/目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40020999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to monitor folder/directory in spring?
提问by amkz
I wan't to write Spring Boot Application in spring which will be monitoring directory in windows, and when I change sub folder or add new one or delete existing one I wanna get information about that.
我不想在 spring 中编写 Spring Boot 应用程序,它将监视 Windows 中的目录,当我更改子文件夹或添加新文件夹或删除现有文件夹时,我想获取有关该文件夹的信息。
How can i do that? I have read this one: http://docs.spring.io/spring-integration/reference/html/files.htmland each result under 'spring file watcher' in google, but I can't find solution...
我怎样才能做到这一点?我读过这个:http: //docs.spring.io/spring-integration/reference/html/files.html以及谷歌“spring file watcher”下的每个结果,但我找不到解决方案......
Do you have a good article or example with something like this? I wan't it to like like this:
你有这样的好文章或例子吗?我不想它喜欢这样:
@SpringBootApplication
@EnableIntegration
public class SpringApp{
public static void main(String[] args) {
SpringApplication.run(SpringApp.class, args);
}
@Bean
public WatchService watcherService() {
...//define WatchService here
}
}
Regards
问候
回答by S?awomir Czaja
You can use pure java for this no need for spring https://docs.oracle.com/javase/tutorial/essential/io/notification.html
您可以为此使用纯 Java,无需 spring https://docs.oracle.com/javase/tutorial/essential/io/notification.html
回答by Gary Russell
See the Spring Integration Samples Repothere's a file sample under 'basic'.
请参阅Spring 集成示例存储库,在“基本”下有一个文件示例。
There's a more recent and more sophisticated sample under applications file-split-ftp
- it uses Spring Boot and Java configuration Vs. the xml used in the older sample.
应用程序下有一个更新和更复杂的示例file-split-ftp
- 它使用 Spring Boot 和 Java 配置 Vs。旧示例中使用的 xml。
回答by Hitham S. AlQadheeb
spring-boot-devtools
has FileSystemWatcher
spring-boot-devtools
拥有 FileSystemWatcher
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
FileWatcherConfig
文件观察器配置
@Configuration
public class FileWatcherConfig {
@Bean
public FileSystemWatcher fileSystemWatcher() {
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(true, Duration.ofMillis(5000L), Duration.ofMillis(3000L));
fileSystemWatcher.addSourceFolder(new File("/path/to/folder"));
fileSystemWatcher.addListener(new MyFileChangeListener());
fileSystemWatcher.start();
System.out.println("started fileSystemWatcher");
return fileSystemWatcher;
}
@PreDestroy
public void onDestroy() throws Exception {
fileSystemWatcher().stop();
}
}
MyFileChangeListener
我的文件更改监听器
@Component
public class MyFileChangeListener implements FileChangeListener {
@Override
public void onChange(Set<ChangedFiles> changeSet) {
for(ChangedFiles cfiles : changeSet) {
for(ChangedFile cfile: cfiles.getFiles()) {
if((cfile.getType().equals(Type.MODIFY) || cfile.getType().equals(Type.ADD)) && !isLocked(cfile.getFile().toPath())) {
System.out.println("Done writing: "+cfile.getFile().getName());
}
}
}
}
private boolean isLocked(Path path) {
try (FileChannel ch = FileChannel.open(path, StandardOpenOption.WRITE); FileLock lock = ch.tryLock()) {
return lock == null;
} catch (IOException e) {
return true;
}
}
}
回答by s.ijpma
Without giving the details here a few pointers which might help you out.
在这里不提供详细信息,一些可能对您有所帮助的指示。
You can take the directory WatchService
code from S?awomir Czaja's answer:
您可以WatchService
从 S?awomir Czaja 的回答中获取目录代码:
You can use pure java for this no need for spring https://docs.oracle.com/javase/tutorial/essential/io/notification.html
您可以为此使用纯 Java,无需 spring https://docs.oracle.com/javase/tutorial/essential/io/notification.html
and wrap that code into a runnable task. This task can notify your clients of directory change using the SimpMessagingTemplate
as described here:
Websocket STOMP handle send
并将该代码包装到一个可运行的任务中。此任务可以使用SimpMessagingTemplate
此处所述的方式通知您的客户端目录更改:
Websocket STOMP 句柄发送
Then you can create a scheduler like described here: Schedulingwhich handles the start and reaccurance of your task.
然后你可以创建一个像这里描述的调度程序: 调度它处理你的任务的开始和重新发生。
Don't forget to configure scheduling and websocket support in your mvc-config as well as STOMP support on the client side (further reading here: STOMP over Websocket)
不要忘记在 mvc-config 中配置调度和 websocket 支持以及客户端的 STOMP 支持(进一步阅读:STOMP over Websocket)
回答by Sanket Mehta
Just in case, if somebody is looking for recursive sub-folder watcher, this link may help: How to watch a folder and subfolders for changes
以防万一,如果有人正在寻找递归子文件夹观察者,此链接可能会有所帮助:如何观察文件夹和子文件夹的更改