sankar
sankar

Reputation: 191

Connecting to the server which password contains "@" symbols using ftp protocol

I am trying to download files from a server using FTP protocol in java. By using following URL I am able to connect to the server & download files.

URL url = new URL("ftp://"+user+":"+password+"@"+host+"/"+remoteFile+";type=i"); 

But when my password contains the "@" (ex : soft@2011) symbol it throws the following exception:

  java.net.UnknownHostException: [email protected] 

It is not able to differentiate both "@" symbols.

How can I avoid this problem? Can I use any escape characters to avoid this problem?

Upvotes: 3

Views: 2560

Answers (2)

fge
fge

Reputation: 121702

Try and use URI instead:

final URI ftpURI = new URI("ftp", "user@pass", host, 22, remoteFile, null, null);

Then use:

ftpURI.toURL()

This should normally give you what is expected.

Upvotes: 1

gustafc
gustafc

Reputation: 28865

URI encoding the password (and preferably the user name as well) should work just fine.

URL url = new URL("ftp://" + 
                  URLEncoder.encode(user, "UTF-8") + ":" + 
                  URLEncoder.encode(password, "UTF-8") + "@" + 
                  host + "/" + remoteFile + ";type=i");

Upvotes: 5

Related Questions