Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,303 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 782 people online right now. Registration is fast and FREE... Join Now!




FTP a file from windows machine to linux box

 
Reply to this topicStart new topic

FTP a file from windows machine to linux box

ani342
31 Oct, 2008 - 11:18 AM
Post #1

New D.I.C Head
*

Joined: 31 Oct, 2008
Posts: 1

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 => code.gif

Thanks,
PsychoCoder smile.gif

User is offlineProfile CardPM
+Quote Post


g00se
RE: FTP A File From Windows Machine To Linux Box
31 Oct, 2008 - 04:37 PM
Post #2

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,042



Thanked: 104 times
My Contributions
The server is expecting you to use SSH and you should do, since it's much more secure. You need a Java SSH client API:

http://sourceforge.net/projects/sshtools/
User is offlineProfile CardPM
+Quote Post

pbl
RE: FTP A File From Windows Machine To Linux Box
31 Oct, 2008 - 07:07 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,954



Thanked: 673 times
Dream Kudos: 200
My Contributions
By the way FTP is FTP
the fact that the machines are Windows, Linux, Unix, S38, VMS, Solaris, VM is completly irrelevant
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:03PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month