473,395 Members | 2,006 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,395 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 11319
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.