java 如何在java中提取sftp url sftp://<user>@<host>[:<port>][/<directory>]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33609411/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:54:55  来源:igfitidea点击:

How to extract sftp url sftp://<user>@<host>[:<port>][/<directory>] in java

java

提问by aashishganesh

Below is the S FTP config in properties file where its contains all attribute in a single line.

下面是属性文件中的 S FTP 配置,其中在一行中包含所有属性。

Properties file:

属性文件:

xyz.sftp=sftp://[email protected]:9090/<directory>

Example:

例子:

sftp://<user>@<host>[:<port>][/<directory>]

I want to parse it and get the following values out of this URL:

我想解析它并从此 URL 中获取以下值:

  1. user
  2. host
  3. port
  4. path
  1. 用户
  2. 主持人
  3. 港口
  4. 小路

回答by Kenster

You can use java.net.URIto parse URLs:

您可以使用java.net.URI来解析 URL:

String ss = "sftp://[email protected]:9090/some/path";

URI uri = new URI(ss);
System.out.printf("URI scheme '%s' user '%s' host '%s' port '%s' path '%s'\n",
        uri.getScheme(), uri.getUserInfo(), uri.getHost(),
        uri.getPort(), uri.getPath());

Prints:

印刷:

URI scheme 'sftp' user 'user' host 'india123.systems.in' port '9090' path '/some/path'

URI 方案 'sftp' 用户 'user' 主机 'india123.systems.in' 端口 '9090' 路径 '/some/path'

The java.net.URLclass has similar parsing abilities, but it'll throw an exception in this case because it doesn't recognize the "sftp" scheme. To avoid that, you'd have to register a protocol handler for the scheme. Registering protocol handlers is apparently rather painful to do; this pagedescribes one way to do it.

java.net.URL班也有类似的分析能力,但它会在这种情况下抛出一个异常,因为它不承认“SFTP”方案。为了避免这种情况,您必须为该方案注册一个协议处理程序。注册协议处理程序显然相当痛苦;此页面描述了一种方法。