import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author wanghao
*/
public class Test006 {
public static void main(String[] args) {
String ftpPath = "ftp://Huser:wang@127.0.0.1/xml/212321.xml";
readFtpPath1(ftpPath);
readFtpPath2(ftpPath);
}
/**
* 使用URI类获取ftp地址的信息
* @param ftpPath ftp地址
*/
private static void readFtpPath1(String ftpPath){
try {
URI uri = new URI(ftpPath);
//ftp
String ftp = uri.getScheme();
//Huser:password
String userPwd = uri.getUserInfo();
//127.0.0.1
String host = uri.getHost();
//21
int port = uri.getPort()>0 ? uri.getPort():21;
// /xml/212321.xml
String path = uri.getPath();
System.out.println(uri.toString());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 使用正则获取ftp地址里面的信息
* @param ftpPath ftp地址
*/
private static void readFtpPath2(String ftpPath) {
String pattern = "(ftp://)(.*?)(:)(.*?)(@)(.*?)(/.*)";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(ftpPath);
if (matcher.find()) {
String name = matcher.group(2);
String password = matcher.group(4);
//127.0.0.1:21
String ipPort = matcher.group(6);
String path = matcher.group(7);
}else{
System.out.println("ftpPath error ");
}
}
}