Problem in doing FTP a directory by java code

Discussion in 'Java' started by ankit.akg007@gmail.com, Feb 8, 2008.

  1. Guest

    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();
    }





    }
     
    , Feb 8, 2008
    #1
    1. Advertising

  2. On Fri, 8 Feb 2008 05:03:09 -0800 (PST), wrote:
    > 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

    --
     
    Gordon Beaton, Feb 8, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. someone

    FTP over SSL vs FTP over SSH

    someone, Apr 24, 2004, in forum: Java
    Replies:
    1
    Views:
    4,610
    iksrazal
    Apr 25, 2004
  2. bhagat
    Replies:
    4
    Views:
    7,797
    steve
    Oct 5, 2005
  3. Replies:
    3
    Views:
    831
  4. Vin Raja

    FTP : Time problem (net/ftp)

    Vin Raja, Jun 7, 2007, in forum: Ruby
    Replies:
    0
    Views:
    105
    Vin Raja
    Jun 7, 2007
  5. D. Buck
    Replies:
    2
    Views:
    527
    D. Buck
    Jun 29, 2004
Loading...

Share This Page