How to implement FTP functionality in Java applications | Code samples and tutorials

Basic location:https://www.toymoban.com/diary/java/363.html 

Implementing FTP functionality in Java applications requires the use of the FTPClient class and related methods. Below is sample code that implements the three main functions:

1) Display files on FTP server:

void ftpList_actionPerformed(ActionEvent e) {
    String server = serverEdit.getText();
    String user = userEdit.getText();
    String password = passwordEdit.getText();
    String path = pathEdit.getText();
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(server);
        ftpClient.login(user, password);
        if (path.length() != 0)
            ftpClient.changeWorkingDirectory(path);
        FTPFile[] files = ftpClient.listFiles();
        for (FTPFile file : files) {
            System.out.println(file.getName());
        }
        ftpClient.logout();
        ftpClient.disconnect();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

2) Download a file from the FTP server:

void getButton_actionPerformed(ActionEvent e) {
    String server = serverEdit.getText();
    String user = userEdit.getText();
    String password = passwordEdit.getText();
    String path = pathEdit.getText();
    String filename = filenameEdit.getText();
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(server);
        ftpClient.login(user, password);
        if (path.length() != 0)
            ftpClient.changeWorkingDirectory(path);
        FileOutputStream outputStream = new FileOutputStream(new File(filename));
        ftpClient.retrieveFile(filename, outputStream);
        outputStream.close();
        ftpClient.logout();
        ftpClient.disconnect();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

3) Upload a file to the FTP server:

void putButton_actionPerformed(ActionEvent e) {
    String server = serverEdit.getText();
    String user = userEdit.getText();
    String password = passwordEdit.getText();
    String path = pathEdit.getText();
    String filename = filenameEdit.getText();
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(server);
        ftpClient.login(user, password);
        if (path.length() != 0)
            ftpClient.changeWorkingDirectory(path);
        FileInputStream inputStream = new FileInputStream(new File(filename));
        ftpClient.storeFile(filename, inputStream);
        inputStream.close();
        ftpClient.logout();
        ftpClient.disconnect();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Please note that the above example code uses the FTPClient class from the Apache Commons Net library. You need to make sure that the relevant library files have been added to the project and included in the code. Additionally, appropriate exception handling and other adjustments may be required depending on your needs and environment setup.

Guess you like

Origin blog.csdn.net/qq_29639425/article/details/133673706