简体   繁体   English

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

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

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://user@india123.systems.in: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 路径

You can use java.net.URI to parse URLs: 您可以使用java.net.URI来解析URL:

String ss = "sftp://user@india123.systems.in: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'用户'用户'主机'india123.systems.in'端口'9090'路径'/ some / path'

The java.net.URL class has similar parsing abilities, but it'll throw an exception in this case because it doesn't recognize the "sftp" scheme. java.net.URL类具有类似的解析能力,但是在这种情况下它将抛出异常,因为它无法识别“ sftp”方案。 To avoid that, you'd have to register a protocol handler for the scheme. 为了避免这种情况,您必须为该方案注册一个协议处理程序。 Registering protocol handlers is apparently rather painful to do; 注册协议处理程序显然很麻烦。 this page describes one way to do it. 此页面描述了一种实现方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM