在 Windows 上使用 Java 检测 Samba 共享上的文件系统更改
对于最近的项目,我需要从 Windows 上的 java 检测映射的 Samba 共享上的文件系统更改:创建、更新和删除。目前,我正在使用文件夹轮询,它维护文件列表及其修改的时间戳来查找事件。
但我的问题是,此文件夹轮询仅在特定时间(例如每 10 秒)查看该文件夹,因此更新的文件可以在两次轮询之间更新两次而不会被识别。
每当 mappep Samba 共享上发生文件事件时,是否有任何方法可以从 Java 内部的窗口获取事件?
谢谢!
for a recent project I need to detect file system changes on a mapped Samba share from java on windows: Creates, updates and removes. At the moment I am using a folder poll that maintains a list of files and their modified timestamp to look for events.
But my problem is that this folder poll only peeks into the folder at certain times (every 10 seconds for example), so an updated file can be updated twice between two polls without recognizing.
Is there any way to get events from windows inside Java whenever a file event occours on that mappep Samba share?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 在当前的 IO api 中没有任何东西来处理来自操作系统的文件通知。
Java 7 将有一个新的 IO API JSR-203,它具有 监视文件 API 将使用本机操作系统通知或轮询(如果不支持检测通知)文件更改。
同时,您可以查看是否有人实现了一个库,允许您通过 JNI 连接到操作系统通知。我认为 IntelliJ IDEA 使用本机库来实现此目的。
在 Sourceforge 上我发现 jfilenotify 但我从未使用过它。
或者,您可以增加采样频率(但这将开始影响您的文件系统)或通过向文件名/文件夹添加版本号来更改文件的写入方式,以便当事情发生变化时你可以很容易地知道。
Java doesn't have anything in the current IO api for dealing with file notifications from the O/S.
Java 7 will have a new IO API JSR-203 that has a watch file API that will either use native O/S notifications or polling if notifications are not supported to detect file changes.
In the meantime you could look to see if someone has implemented a library that allows you to hook into the O/S notifications via JNI. I think that IntelliJ IDEA uses a native lib for this purpose.
Looking on Sourceforge I've found jfilenotify but i've never used it.
Alternatively you could increase your sampling frequency (but this will start to hammer your filesystem) or change how your files are written out by adding a version number to the filename/folder so that you can easily tell when something has changed.
根据 此论坛条目 TeamDev JNIWrapper 能够像我想要的那样执行此操作。我刚刚购买了一本。谢谢你和
格雷兹,
甘德
According to this forum entry TeamDev JNIWrapper is capable of doing this like I want it. I just purchased a copy. Thank you and
Greetz,
GHad