使用ftp协议连接到密码包含“@”符号的服务器
我试图在java中使用FTP协议从服务器下载文件。 通过使用以下URL我能够连接到服务器和下载文件。
URL url = new URL("ftp://"+user+":"+password+"@"+host+"/"+remoteFile+";type=i");
但是当我的密码包含“@”(例如:soft @ 2011)符号时,它会抛出以下exception:
java.net.UnknownHostException: 2010@192.168.1.100
它无法区分两个“@”符号。
我该如何避免这个问题? 我可以使用任何转义字符来避免这个问题吗?
编码密码的URI (最好是用户名)应该可以正常工作。
URL url = new URL("ftp://" + URLEncoder.encode(user, "UTF-8") + ":" + URLEncoder.encode(password, "UTF-8") + "@" + host + "/" + remoteFile + ";type=i");
请尝试使用URI:
final URI ftpURI = new URI("ftp", "user@pass", host, 22, remoteFile, null, null);
然后使用:
ftpURI.toURL()
这应该通常给你预期的。