androidftpapache-commons-netftp-server

Add content to a file in an FTP server without erasing old data?


I'm using the code below to connect to a server and send a file. The problem is that the file may get bigger with time so I want to:

  1. send the file named "file" to the server

  2. the next time I write in the file "file" I'll be erasing the old content and writing new data.

  3. I want the new file which has the same name "file" as the old one but with different new content (since the old content has been written over) to be sent to the server.

Is the following doable: Can I fetch the file "file" which has the same name as the one I'm sending and instead of it being replaced by the new file, the new file would be added at the end of the old file combining both contents?

This is how I'm sending the file to the server:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);
FTPClient con = null;
FileInputStream fileIn= null;
//Find the directory for the SD Card using the API
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file");

try {
    con = new FTPClient();
    con.connect("****************", **);
    showServerReply(con);

    if (con.login("***********", "*******")) {
        con.enterLocalPassiveMode();
        con.setFileType(FTP.BINARY_FILE_TYPE);
        String data=String.valueOf(Environment.getExternalStorageDirectory())+"/file";
        fileIn = new FileInputStream(new File(data));

        boolean result = con.storeFile("/"+android.os.Build.MODEL+".txt", fileIn);
        fileIn.close();
        con.logout();
        con.disconnect();
    }
} catch (Exception e) {
    e.printStackTrace();
}

Solution

  • Just replace your storeFile call with the appendFile.

    boolean result = con.appendFile("/"+android.os.Build.MODEL+".txt", fileIn);