Hi!

How could I create/delete a directory on the remote machine using FTPClient of org.apache.commons.net.ftp.*; ?

The code used to save a file on the remote machine is shown below. But I would like to update it for creating/deleting directories.

Thanks!

private void saveAttachedFileToRemoteMachine(String pathToFile, DefaultMutableTreeNode selectedNode) {

        FTPClient client = new FTPClient();
        FileInputStream fis = null;

        try {
            client.connect(this.url);
            client.login(this.login,this.password);
            client.setFileType(FTP.BINARY_FILE_TYPE);

            String directoryName = new String();
            Object elements[] = selectedNode.getPath();
            for (int i = 0, n = elements.length; i < n; i++) {
               directoryName = directoryName + elements[i] + "\\";
            }

            String filename = directoryName + text4.getText();
            fis = new FileInputStream(pathToFile);

            client.storeFile(filename, fis);
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

I'll try client.makeDirectory(pathToFile);...

Hmm... It's strange that "boolean b = client.makeDirectory(directoryPath);" returns "false". I checked that the path defined in "directoryPath" exists (let's say C:\Documents\). Why a new directory is not created?

I solved the problem. The path should start from the root directory, let's say "\\Documents\\". But I had "C:\\Documents\\"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.