How to copy a file on the FTP server to a directory on the same server in Java?

前端 未结 4 903
长情又很酷
长情又很酷 2020-12-05 08:25

I\'m using Apache Commons FTP to upload a file. Before uploading I want to check if the file already exists on the server and make a backup from it to a backup directory on

4条回答
  •  独厮守ぢ
    2020-12-05 09:04

    Try this way,

    I am using apache's library .

    ftpClient.rename(from, to) will make it easier, i have mentioned in the code below where to add ftpClient.rename(from,to).

    public void goforIt(){
    
    
            FTPClient con = null;
    
            try
            {
                con = new FTPClient();
                con.connect("www.ujudgeit.net");
    
                if (con.login("ujud3", "Stevejobs27!!!!"))
                {
                    con.enterLocalPassiveMode(); // important!
                    con.setFileType(FTP.BINARY_FILE_TYPE);
                    String data = "/sdcard/prerakm4a.m4a";
                    ByteArrayInputStream(data.getBytes());
                    FileInputStream in = new FileInputStream(new File(data));
                    boolean result = con.storeFile("/Ads/prerakm4a.m4a", in);
                    in.close();
                    if (result) 
                           {
                                Log.v("upload result", "succeeded");
    

    //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Add the backup Here$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//

                       // Now here you can store the file into a backup location
    
                      // Use ftpClient.rename(from, to) to place it in backup
    

    //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Add the backup Here$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//

                           }
                    con.logout();
                    con.disconnect();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }   
    
        }
    

提交回复
热议问题