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

前端 未结 4 901
长情又很酷
长情又很酷 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 08:45

    If you are using apache commons net FTPClient, there is a direct method to move a file from one location to another location (if the user has proper permissions).

    ftpClient.rename(from, to);
    

    or, If you are familiar with ftp commands, you can use something like

    ftpClient.sendCommand(FTPCommand.yourCommand, args);
    if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
         //command successful;
    } else {
         //check for reply code, and take appropriate action.
    }
    

    If you are using any other client, go through the documentation, There wont be much changes between client implementations.

    UPDATE:

    Above approach moves the file to to directory, i.e, the file won't be there in from directory anymore. Basically ftp protocol meant to be transfer the files from local <-> remote or remote <-> other remote but not to transfer with in the server.

    The work around here, would be simpler, get the complete file to a local InputStream and write it back to the server as a new file in the back up directory.

    to get the complete file,

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ftpClient.retrieveFile(fileName, outputStream);
    InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
    

    now, store this stream to backup directory. First we need to change working directory to backup directory.

    // assuming backup directory is with in current working directory
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//binary files
    ftpClient.changeWorkingDirectory("backup");
    //this overwrites the existing file
    ftpClient.storeFile(fileName, is);
    //if you don't want to overwrite it use storeUniqueFile
    

    Hope this helps you..

    0 讨论(0)
  • 2020-12-05 08:55

    To backup at same Server (move), can you use:

    String source="/home/user/some";
    String goal  ="/home/user/someOther";
    FTPFile[] filesFTP = cliente.listFiles(source);
    
    clientFTP.changeWorkingDirectory(goal);  // IMPORTANT change to final directory
    
    for (FTPFile f : archivosFTP) 
       {
        if(f.isFile())
           {
            cliente.rename(source+"/"+f.getName(), f.getName());
           }
       }
    
    0 讨论(0)
  • 2020-12-05 09:03

    There's no standard way to duplicate a remote file over FTP protocol. Some FTP servers support proprietary or non-standard extensions for this though.


    So if your are lucky that your server is ProFTPD with mod_copy module, you can use FTP.sendCommand to issue these two commands:

    f.sendCommand("CPFR sourcepath");
    f.sendCommand("CPTO targetpath");
    

    The second possibility is that your server allows you to execute arbitrary shell commands. This is even less common. If your server supports this you can use SITE EXEC command:

    SITE EXEC cp -p sourcepath targetpath
    

    Another workaround is to open a second connection to the FTP server and make the server upload the file to itself by piping a passive mode data connection to an active mode data connection. Implementation of this solution (in PHP though) is shown in FTP copy a file to another place in same FTP.


    If neither of this works, all you can do is to download the file to a local temporary location and re-upload it back to the target location. This is that the answer by @RP- shows.


    See also FTP copy a file to another place in same FTP.

    0 讨论(0)
  • 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();
            }   
    
        }
    
    0 讨论(0)
提交回复
热议问题