Problem in doing FTP a directory by java code

A

ankit.akg007

Hello Frnds


I was trying to do ftp using sun.net.ftp.*; but while doing ftp its
not upload directories in proper structural way inspite of it it just
uploading the files which are in nested directory altogether
means i am not getting same directory structure as on client side....

Please Help me...
Here is the code attached


import java.io.*;
import sun.net.ftp.*;

class FTPServer{
private String path="/C/remote";
//private String path="C:\\a_h\\cmaf2\\repository\\masterRepository\
\srcRepository" ;
private String userName="superuser";
private String passWd="nopasswd";
public static int BUFFER_SIZE = 10240;
FtpClient fc;


public void connect()
{
try{
fc=new FtpClient("192.168.61.84");
fc.login(userName,passWd);
System.out.println("Connected");
fc.cd(path);
System.out.println("Path Changed");
fc.binary();
}
catch(Exception e)
{
e.printStackTrace();
}

}

public void disconnect()
{
if(null!=fc)
{
try{
fc.cd("/");
fc.closeServer();
System.out.println("DisConnected");
}
catch(Exception e){
e.printStackTrace();
}

}
}
public void putFile(String file_name)
{
try {
byte[] buffer = new byte[BUFFER_SIZE];
File file=new File(file_name);


FileInputStream in = new
FileInputStream(file_name);
String
file_separator=System.getProperty("file.separator");
int
last_index=file_name.lastIndexOf(file_separator);
OutputStream out =
fc.put(file_name.substring(last_index+1));

int counter = 0;
while (true) {
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
System.out.println(counter);
}

out.flush();
out.close();
in.close();
}
catch(Exception e)
{
e.printStackTrace();

}



}

public void visitAllDirsAndFiles(File dir) {


if (dir.isDirectory()) {
//putFile(dir.getAbsolutePath());
File[] children = dir.listFiles();
for (int i=0; i<children.length; i++) {
visitAllDirsAndFiles(children);
}
// exiting from the directory
}else
{
String fileName=dir.getAbsolutePath();
putFile(fileName);
//System.out.println(fileName);

}
}
public static void main(String args[]){

FTPServer myserver=new FTPServer();
myserver.connect();
myserver.visitAllDirsAndFiles(new File("C:\\ULDC"));
myserver.disconnect();
}





}
 
G

Gordon Beaton

I was trying to do ftp using sun.net.ftp.*; but while doing ftp its
not upload directories in proper structural way inspite of it it
just uploading the files which are in nested directory altogether
means i am not getting same directory structure as on client
side....

Instead of doing "put" with an absolute path+filename, probably you
should use fc.cd() to go to the appropriate directory first, and then
do "put" with the basename of the file to upload. You will likely need
to create the directories on the server too if they don't already
exist.

Otherwise, you might find this to be a better choice of ftp client,
since AFAIK the Sun library is undocumented and unsupported:

http://commons.apache.org/net/

/gordon

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top