Can any one let me know what is th easy way to ftp file from windows to linux machine using java net package.
With the java program mentioned below I am getting below error
Error: org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-1.99-OpenSSH_3.9p1
I am able to ftp the same file using winscp. but when I try to ftp from my java program it's throwing above error. I am using commons-net-1.0.0.jar to ftp files.
Here is the java program I am using.
java
try {
InetAddress ftpHost = InetAddress.getByName("10.10.10.10");
String ftpUserName = "test";
String ftpPassword = "test";
String ftpRemoteDirectory = "/home/tarcher/tmp/ ";
String fileToTransmit = "C:\\sample.txt";
//Create a Jakarta Commons Net FTP Client object
FTPClient ftp = new FTPClient();
//A datatype to store responses from the FTP server
int reply;
//Connect to the server
System.out.println("1, "+ftpHost.getHostName()+", "+ftpHost);
ftp.connect(ftpHost.getHostName(), 22);
// After connection attempt, you should check the reply code to verify success.
reply = ftp.getReplyCode();
System.out.println(reply);
if(!FTPReply.isPositiveCompletion(reply)) {
try {
ftp.disconnect();
} catch (Exception e) {
System.err.println("Unable to disconnect from FTP server " + "after server refused connection. "+e.toString());
}
throw new Exception ("FTP server refused connection.");
}
System.out.println("Connected to " + ftpHost + ". "+ftp.getReplyString());
//Try to login
if (!ftp.login(ftpUserName, ftpPassword)) {
throw new Exception ("Unable to login to FTP server " + "using username "+ftpUserName+" " + "and password "+ftpPassword);
}
System.out.println(ftp.getReplyString());
System.out.println("Remote system is " + ftp.getSystemName());
//Set our file transfer mode to either ASCII or Binary
//ftp.setFileType(FTP.ASCII_FILE_TYPE);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//Change the remote directory
if (ftpRemoteDirectory != null && ftpRemoteDirectory.trim().length() > 0) {
System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);
ftp.changeWorkingDirectory(ftpRemoteDirectory);
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
throw new Exception ("Unable to change working directory " + "to:"+ftpRemoteDirectory);
}
}
//Get the file that we will transfer and send it.
File f = new File(fileToTransmit);
System.out.println("Storing file as remote filename: " + f.getName());
boolean retValue = ftp.storeFile(f.getName(), new FileInputStream(f));
if (!retValue) {
throw new Exception ("Storing of remote file failed. ftp.storeFile()" + " returned false.");
}
//Disconnect from the FTP server
try {
//ftp.logout();
ftp.disconnect();
} catch (Exception exc) {
System.err.println("Unable to disconnect from FTP server. " + exc.toString());
}
} catch (Exception e) {
System.err.println("Error: "+e.toString());
}
System.out.println("Process Complete.");
System.exit(0);
Mod Edit: Please use code tags when posting your code. Code tags are used like so =>

Thanks,
PsychoCoder