With the java program mentioned below I am getting below error
Error: org.apache.commons.net.MalformedServerReplyExcepti on: 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.
Expand|Select|Wrap|Line Numbers
- try {
- 02
- InetAddress ftpHost = InetAddress.getByName("10.10.10.10");
- 03
- String ftpUserName = "test";
- 04
- String ftpPassword = "test";
- 05
- String ftpRemoteDirectory = "/home/tarcher/tmp/ ";
- 06
- String fileToTransmit = "C:\\sample.txt";
- 07
- 08
- //Create a Jakarta Commons Net FTP Client object
- 09
- FTPClient ftp = new FTPClient();
- 10
- //A datatype to store responses from the FTP server
- 11
- int reply;
- 12
- //Connect to the server
- 13
- System.out.println("1, "+ftpHost.getHostName()+", "+ftpHost);
- 14
- ftp.connect(ftpHost.getHostName(), 22);
- 15
- // After connection attempt, you should check the reply code to verify success.
- 16
- reply = ftp.getReplyCode();
- 17
- System.out.println(reply);
- 18
- if(!FTPReply.isPositiveCompletion(reply)) {
- 19
- try {
- 20
- ftp.disconnect();
- 21
- } catch (Exception e) {
- 22
- System.err.println("Unable to disconnect from FTP server " + "after server refused connection. "+e.toString());
- 23
- }
- 24
- throw new Exception ("FTP server refused connection.");
- 25
- }
- 26
- System.out.println("Connected to " + ftpHost + ". "+ftp.getReplyString());
- 27
- 28
- //Try to login
- 29
- if (!ftp.login(ftpUserName, ftpPassword)) {
- 30
- throw new Exception ("Unable to login to FTP server " + "using username "+ftpUserName+" " + "and password "+ftpPassword);
- 31
- }
- 32
- System.out.println(ftp.getReplyString());
- 33
- System.out.println("Remote system is " + ftp.getSystemName());
- 34
- 35
- //Set our file transfer mode to either ASCII or Binary
- 36
- //ftp.setFileType(FTP.ASCII_FILE_TYPE);
- 37
- ftp.setFileType(FTP.BINARY_FILE_TYPE);
- 38
- 39
- //Change the remote directory
- 40
- if (ftpRemoteDirectory != null && ftpRemoteDirectory.trim().length() > 0) {
- 41
- System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);
- 42
- ftp.changeWorkingDirectory(ftpRemoteDirectory);
- 43
- reply = ftp.getReplyCode();
- 44
- if(!FTPReply.isPositiveCompletion(reply)) {
- 45
- throw new Exception ("Unable to change working directory " + "to:"+ftpRemoteDirectory);
- 46
- }
- 47
- }
- 48
- 49
- //Get the file that we will transfer and send it.
- 50
- File f = new File(fileToTransmit);
- 51
- System.out.println("Storing file as remote filename: " + f.getName());
- 52
- boolean retValue = ftp.storeFile(f.getName(), new FileInputStream(f));
- 53
- if (!retValue) {
- 54
- throw new Exception ("Storing of remote file failed. ftp.storeFile()" + " returned false.");
- 55
- }
- 56
- 57
- //Disconnect from the FTP server
- 58
- try {
- 59
- //ftp.logout();
- 60
- ftp.disconnect();
- 61
- } catch (Exception exc) {
- 62
- System.err.println("Unable to disconnect from FTP server. " + exc.toString());
- 63
- }
- 64
- } catch (Exception e) {
- 65
- System.err.println("Error: "+e.toString());
- 66
- }
- 67
- System.out.println("Process Complete.");
- 68
- System.exit(0);