473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to copy a file from Windows system to Linux System using Java program

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.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
  1. try {
  2. 02
  3.             InetAddress ftpHost = InetAddress.getByName("10.10.10.10");
  4. 03
  5.             String ftpUserName = "test";
  6. 04
  7.             String ftpPassword = "test";
  8. 05
  9.             String ftpRemoteDirectory = "/home/tarcher/tmp/ ";
  10. 06
  11.             String fileToTransmit = "C:\\sample.txt";
  12. 07
  13.  
  14. 08
  15.             //Create a Jakarta Commons Net FTP Client object
  16. 09
  17.             FTPClient ftp = new FTPClient();
  18. 10
  19.             //A datatype to store responses from the FTP server
  20. 11
  21.             int reply;
  22. 12
  23.             //Connect to the server
  24. 13
  25.             System.out.println("1, "+ftpHost.getHostName()+", "+ftpHost);
  26. 14
  27.             ftp.connect(ftpHost.getHostName(), 22);
  28. 15
  29.             // After connection attempt, you should check the reply code to verify success.
  30. 16
  31.             reply = ftp.getReplyCode();  
  32. 17
  33.             System.out.println(reply);
  34. 18
  35.             if(!FTPReply.isPositiveCompletion(reply)) {
  36. 19
  37.                 try {
  38. 20
  39.                     ftp.disconnect();
  40. 21
  41.                 } catch (Exception e) {
  42. 22
  43.                     System.err.println("Unable to disconnect from FTP server " + "after server refused connection. "+e.toString());
  44. 23
  45.                 }
  46. 24
  47.                 throw new Exception ("FTP server refused connection.");
  48. 25
  49.             }           
  50. 26
  51.             System.out.println("Connected to " + ftpHost + ". "+ftp.getReplyString());
  52. 27
  53.  
  54. 28
  55.             //Try to login
  56. 29
  57.             if (!ftp.login(ftpUserName, ftpPassword)) {
  58. 30
  59.                 throw new Exception ("Unable to login to FTP server " + "using username "+ftpUserName+" " + "and password "+ftpPassword);
  60. 31
  61.             }
  62. 32
  63.             System.out.println(ftp.getReplyString());
  64. 33
  65.             System.out.println("Remote system is " + ftp.getSystemName());
  66. 34
  67.  
  68. 35
  69.             //Set our file transfer mode to either ASCII or Binary
  70. 36
  71.             //ftp.setFileType(FTP.ASCII_FILE_TYPE);
  72. 37
  73.             ftp.setFileType(FTP.BINARY_FILE_TYPE);
  74. 38
  75.  
  76. 39
  77.             //Change the remote directory
  78. 40
  79.             if (ftpRemoteDirectory != null && ftpRemoteDirectory.trim().length() > 0) {
  80. 41
  81.                 System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);
  82. 42
  83.                 ftp.changeWorkingDirectory(ftpRemoteDirectory);
  84. 43
  85.                 reply = ftp.getReplyCode();
  86. 44
  87.                 if(!FTPReply.isPositiveCompletion(reply)) {
  88. 45
  89.                     throw new Exception ("Unable to change working directory " + "to:"+ftpRemoteDirectory);
  90. 46
  91.                 }
  92. 47
  93.             }
  94. 48
  95.  
  96. 49
  97.             //Get the file that we will transfer and send it.
  98. 50
  99.             File f = new File(fileToTransmit);
  100. 51
  101.             System.out.println("Storing file as remote filename: " + f.getName());
  102. 52
  103.             boolean retValue = ftp.storeFile(f.getName(), new FileInputStream(f));
  104. 53
  105.             if (!retValue) {
  106. 54
  107.               throw new Exception ("Storing of remote file failed. ftp.storeFile()" + " returned false.");
  108. 55
  109.             }
  110. 56
  111.  
  112. 57
  113.             //Disconnect from the FTP server
  114. 58
  115.             try {
  116. 59
  117.                 //ftp.logout();
  118. 60
  119.                 ftp.disconnect();
  120. 61
  121.             } catch (Exception exc) {
  122. 62
  123.                 System.err.println("Unable to disconnect from FTP server. " + exc.toString());
  124. 63
  125.             }
  126. 64
  127.         } catch (Exception e) {
  128. 65
  129.             System.err.println("Error: "+e.toString());
  130. 66
  131.         }
  132. 67
  133.         System.out.println("Process Complete.");
  134. 68
  135.         System.exit(0);
Apr 1 '14 #1
1 11294
Luuk
1,047 Expert 1GB
Why are you using the 1.0.0 version of commons-net-1.0.0.jar?

There is also a 3.3 version of the same available.....
(http://commons.apache.org/proper/com...wnload_net.cgi)

It might help to repost your code without line numbers,
and telling on which line you got the exception. (check the line number with the line number you get after posting the code)
Apr 1 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: SagharAyyaz | last post by:
Hi all, I am new to google groups. I want to copy files from Windows to Linux Server through VC++ program. Can any body help me? I don't know, am I in the right group or not but hopefully I...
0
by: ybarwani | last post by:
i need help to solve this program buy using Java programe javaScript and to know the cods Make the output showing the randomly circle with diffrent colors ybarwani@gmail.com
0
by: nehacredo | last post by:
hi alll.. i wanna display a word file content in a browser..how can i do that..i hav done one coding in that i can display the contents...but with contents some symbols also coming... anyone pls...
0
by: sriharim | last post by:
Hi ALL How to read and write windowsini format .ini files using Java. I would like to parse complex elements. ini file fromat would be like SESSION = { name="session1", service="7000", ...
5
by: Sekkappan | last post by:
hi, I am new to javamail.now i urgently want a code to send a mail through java program.i am using struts application. can any one explain me clearly about procedure to send to using java...
2
by: Giridhar Sanapala | last post by:
Can any one tel me the procedure to mail using java program............
1
by: ojhamuke | last post by:
Hi, I want to create a java program which will copy or move files from windows machine to linux machine directory. I tried by socket connection. Connection is successful but I want to paste windows...
0
by: raana syeda | last post by:
Please tell me how to get the Windows Xp system32 path using java.I m using java1.6.0
1
by: Shun | last post by:
Hello, I am trying to extract the data from my Oracle 10g database using Java. First, I tried to connect Oracle to Java using this code and it works. import java.util.*; import java.sql.*;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.