使用Java NIO监视服务监视http资源
我需要监视AmazonS3上的一个目录,以检查是否有任何新文件添加到此目录。我尝试使用JavaNioWatch服务,但是它不能正常工作。如果我对提供的S3路径使用以下语法:使用Java NIO监视服务监视http资源,java,amazon-s3,nio,watchservice,Java,Amazon S3,Nio,Watchservice,我需要监视AmazonS3上的一个目录,以检查是否有任何新文件添加到此目录。我尝试使用JavaNioWatch服务,但是它不能正常工作。如果我对提供的S3路径使用以下语法: String pathToMonitor="file://https://abc/dir"; //Line1 Path path=Paths.get(new URL(pathToMonitor).toURI()); //Line2 Boolean isFolder=(Boolean) Files.getAttribute(
String pathToMonitor="file://https://abc/dir"; //Line1
Path path=Paths.get(new URL(pathToMonitor).toURI()); //Line2
Boolean isFolder=(Boolean) Files.getAttribute(file, "basic:isDirectory", LinkOption.NOFOLLOW_LINKS); //Line3
然后我得到以下错误:
java.nio.file.FileSystemException: \\https\abc\dir: The network path was not found.
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:53)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:38)
at sun.nio.fs.AbstractBasicFileAttributeView.readAttributes(AbstractBasicFileAttributeView.java:168)
at sun.nio.fs.AbstractFileSystemProvider.readAttributes(AbstractFileSystemProvider.java:92)
at java.nio.file.Files.readAttributes(Files.java:1961)
at java.nio.file.Files.getAttribute(Files.java:1866)
如果从路径中删除文件://
前缀,则会生成以下错误:
Exception in thread "main" java.nio.file.FileSystemNotFoundException: Provider "https" not installed
at java.nio.file.Paths.get(Paths.java:147)
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 5: https://abc/dir
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.nio.file.Paths.get(Paths.java:84)
如果我将'Line2'修改为Path=Path.get(“https://abc/dir");代码>然后生成以下跟踪:
Exception in thread "main" java.nio.file.FileSystemNotFoundException: Provider "https" not installed
at java.nio.file.Paths.get(Paths.java:147)
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 5: https://abc/dir
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.nio.file.Paths.get(Paths.java:84)
线程“main”java.nio.file.InvalidPathException中的异常:索引5处的非法字符:https://abc/dir
位于sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
位于sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
位于sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
位于sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
位于sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
位于java.nio.file.Paths.get(path.java:84)
请让我知道我在这里做错了什么,以及是否可以使用Java watch服务监控此类web资源,或者是否有任何其他框架/api
谢谢您将
文件
协议和http
协议混合在一起,这没有什么意义
基本上,如果您能够访问资源的唯一方式是通过HTTP,那么您无法完成您试图完成的任务。当HTTP资源发生更改时,没有通用的通知机制,因为没有通用的机制从HTTP资源发出推送通知,而且它不在操作系统的控制范围内,因此无法在更改发生时拦截。对于本地文件,您的操作系统可以在更改发生时检测到更改,因为它最终负责处理对本地磁盘的写入,但这不适用于您的情况
您需要一些轮询更改的东西,除非S3有一些定制的东西来推送更改通知(但这是您必须单独调查的事情)。Java NIO无法做到这一点。请查看Chiatic security:感谢您的回复。这真的很有帮助。我将尝试执行您建议的投票程序。