Just in case i do keep forgetting that stuff, here’s a regex for decoding urls like
ftp://user:somepass@somehost:someport
in Java:
final Hashtable<String, Integer> portMap = new Hashtable<String, Integer>(); portMap.put("ftp", 21); portMap.put("sftp", 22); final Pattern urlPattern = Pattern.compile("(ftp|sftp)://(\\S+):(\\S+)@([\\S&&[^:]]+)(:(\\d+))?"); final Matcher m = urlPattern.matcher(url); if(!m.matches()) throw new RuntimeException("Invalid ftp url!"); final String protocol = m.group(1).toLowerCase(); final String user = m.group(2); final String password = m.group(3); final String host = m.group(4); final int port = m.group(6) != null ? Integer.parseInt(m.group(6)) : portMap.get(protocol); |
Just in case anybody is interessted, i’m writing a wrapper around j2ssh and Commons::Net to support both ftp and sftp in a J2SE program.
One comment
Your post was useful for me, and I want to thank you for that. Because in my case I was trying to apply it for http/https protocols, I hit the situation were there can be a port number. Like ‘8080’ in http://anywhere.com:8080. So I had to adapt your regex pattern. But please note that according to https://www.w3.org/Addressing/URL/uri-spec.html, you could have a port specified in an ftp uri, too. Greetings from Spain!
Post a Comment