检查远程服务器上的路径是否为目录 JSch

huangapple go评论61阅读模式
英文:

Check if a path in remote server is a directory JSch

问题

如何检查SFTP远程服务器上的路径是目录还是文件,我可以检查路径是否以扩展名结尾,例如:

  • something/something.txt 如果以扩展名结尾,返回false,表示它不是目录。
  • something/something 如果没有扩展名,返回true,表示它是目录。

但在技术上,有些文件没有扩展名,程序会认为它是目录,而这种方法并不是最好的。是否有其他方法来检查路径是否为目录?我正在使用JSch库。

英文:

How would i check if a path in a SFTP remote server is a directory or file, i could check if the path ends with an extension e.g

  • something/something.txt false because it has an extension which means it's not a directory
  • something/something true because it doesn't have an extension which means it's a directory

but technically there are files that has no extension and the program will think it's a directory, and this method isn't really the best, is there another way to check if a path is a directory. I'm using the JSch library

答案1

得分: 0

你尝试过旧的 java.io.File#isDirectory() 吗?或者 java.nio.file.Files.isDirectory()

很可能你可以从 URL 获取一个 FilePath。有一些构造函数可以指定协议和主机,比如 URL​(String protocol, String host, int port, String file)

现在你告诉我有关 ChannelSftp(我不了解 JSch)的信息。我找到了一个有用的方法:
stat(),你可以调用 sftpATTRS.isDir()

channel.stat(filePath).isDir()

英文:

Have you tried the good old java.io.File#isDirectory()? Or java.nio.file.Files.isDirectory()?

Most probably you could get a File or Path from a URL. There are constructors which specify the protocol and host, like URL​(String protocol, String host, int port, String file)


Now that you told me about ChannelSftp (I don't know JSch). I have found a method which will help:
stat() and you can call sftpATTRS.isDir():

channel.stat(filePath).isDir()

huangapple
  • 本文由 发表于 2020年8月10日 04:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定