try {
ftpClient.connect(host, port);
} catch (Exception e) {
final String url = ftpUrl.toString();
final String msg = e.getMessage();
throw new ResourceDownloadError(url, msg, e);
}
//login to user account
String userInfo = ftpUrl.getUserInfo();
String username = DEFAULT_USER_NAME;
String password = "";
if (userInfo != null) {
if (userInfo.contains(":")) {
//provided username & password so parse
String[] userInfoTokens = userInfo.split("\\:");
if (userInfoTokens.length == 2) {
username = userInfoTokens[0];
password = userInfoTokens[1];
}
} else {
//provided only username
username = userInfo;
//prompt for password
char[] pwd;
try {
pwd =
PasswordPrompter.getPassword(pwdInputStream,
"Connecting to '" + ftpUrl.toString()
+ "'. Enter password for user '"
+ username + "': ");
} catch (IOException e) {
final String name = ftpUrl.toString();
final String msg = e.getMessage();
throw new ResourceDownloadError(name, msg, e);
}
if (pwd == null) {
password = "";
} else {
password = String.valueOf(pwd);
}
}
}
try {
if (!ftpClient.login(username, password)) {
final String name = ftpUrl.toString();
final String msg = "Login error for username and password";
throw new ResourceDownloadError(name, msg);
}
} catch (IOException e) {
final String name = ftpUrl.toString();
final String msg = "Login error for username and password";
throw new ResourceDownloadError(name, msg, e);
}
try {
ftpClient.pasv();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
} catch (IOException e) {
final String url = ftpUrl.toString();
final String msg = "Error setting passive mode or transfer type";
throw new ResourceDownloadError(url, msg, e);
}
}