Hiển thị danh sách, sắp xếp file | FTP Java Socket

Xây dựng chương trình Java kết nối tới máy chủ FTP Server và hiển thị danh sách file theo ngày, kích thước.

Xem thêm:

Kiểm tra n được gửi từ Client có phải là số hoàn hảo

chuyển một số sang hệ Hexa-Decimal java socket

1. Mô tả bài toán

Viết chương trình FTP Client thực hiện trao đổi dữ liệu giữa Client và Server như sau:

Hiển thị danh sách các tệp tin trên máy chủ bao gồm: tên, kích thước, ngày tạo.

Tạo 2 button sắp xếp theo kích thước và sắp xếp theo ngày tạo.

Video hướng dẫn chi tiết:

2. Code tham khảo

public static final String SERVER_IP = "192.168.30.140";
public static final int SERVER_PORT = 21;
public static final String USER = "user1";
public static final String PASS = "P@ssword";
FTPClient client;

public GetFile() {
    initComponents();
    client = new FTPClient();
}

2.1 Kết nối với thư mục FTP Server Java Socket

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    txtListFile.setText("");
    try {
        client.connect(SERVER_IP, SERVER_PORT);
        int replyCode = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(replyCode)) {
            txtListFile.setText("Fail. Server reply code: " + replyCode);
        }

        boolean status = client.login(USER, PASS);

        if (status) {
            txtListFile.setText("Login Successfull");
        } else {
            txtListFile.setText("Login Fail");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2.2 Hiển thị danh sách các tệp tin trên FTP Server

private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {                                        
    txtListFile.setText("");
    try {
        FTPFile[] fTPFiles = client.listFiles();
        String s = "";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        for (FTPFile f : fTPFiles) {
            if (f.getType() == FTPFile.FILE_TYPE) {
                LocalDateTime time = LocalDateTime.ofInstant(f.getTimestamp().toInstant(), ZoneId.systemDefault());
                s += f.getName() + "    " + f.getSize() + "    " + dtf.format(time) + "\n";
            }
        }
        txtListFile.setText(s);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2.3 Chương trình con hiển thị danh sách file ftp

public static void showFile(FTPClient client, ArrayList<FTPFile> list, JTextArea txtListFile) {
    try {
        String s = "";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        for (FTPFile f : list) {
            if (f.getType() == FTPFile.FILE_TYPE) {
                LocalDateTime time = LocalDateTime.ofInstant(f.getTimestamp().toInstant(), ZoneId.systemDefault());
                s += f.getName() + "    " + f.getSize() + "    " + dtf.format(time) + "\n";
            }
        }
        txtListFile.setText(s);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

2.4 Sắp xếp các tệp tin theo ngày tháng

private void btnDateActionPerformed(java.awt.event.ActionEvent evt) {                                        
    txtListFile.setText("");
    try {
        FTPFile[] fTPFiles = client.listFiles();
        ArrayList<FTPFile> list = new ArrayList<>();

        for (FTPFile f : fTPFiles) {
            list.add(f);
        }

        int listSize = list.size();

        for (int i = 0; i < listSize; i++) {
            for (int j = i + 1; j < listSize; j++) {
                if (list.get(i).getTimestamp().compareTo(list.get(j).getTimestamp()) < 0) {
                    Collections.swap(list, i, j);
                }
            }
        }
        showFile(client, list, txtListFile);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

2.5 Sắp xếp file theo kích thước

private void btnSizeActionPerformed(java.awt.event.ActionEvent evt) {                                        
    txtListFile.setText("");
    try {
        FTPFile[] fTPFiles = client.listFiles();
        ArrayList<FTPFile> list = new ArrayList<>();

        for (FTPFile f : fTPFiles) {
            list.add(f);
        }

        int listSize = list.size();

        for (int i = 0; i < listSize; i++) {
            for (int j = i + 1; j < listSize; j++) {
                if (list.get(i).getSize() > list.get(j).getSize()) {
                    Collections.swap(list, i, j);
                }
            }
        }
        showFile(client, list, txtListFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Bài viết liên quan:

phân tích n thành tích các số nguyên tố java socket

server gửi chuỗi đảo ngược cho client

Server chuyển xâu thành chữ in hoa rồi gửi lại Client

Nguyễn Tiến Trường

Mình viết về những điều nhỏ nhặt trong cuộc sống, Viết về câu chuyện những ngày không có em