實作FTP客戶端

範例中的程式建立於JAVA視窗界面建立FTP連線,並且以視覺化選取檔案並上傳。Swing客戶端應用程序通過FTP協議連接到服務器以傳輸文件。因為要使用Apache提供的FTP套件,無論是以傳統導入套件方式Maven方式都有繁雜的過程。請先建立名為「ftpclient」專案下載此範例置換掉workspace中的同名資料夾即可。
下載



建立界面

請建立名為FtpClient的視窗,方法如下。

請拖拉視窗工具完成下方程式界面,方法如下。


請在大約19行的地方加入程式碼,方法如下。
1
private JFileChooser filePicker;





請在大約88行的地方加入程式碼,方法如下。
1
2
3
filePicker = new JFileChooser();
filePicker.setBounds(10, 180, 200,200);
contentPane.add(filePicker);


請將檔案選擇工具調整至適當大小,並增加一個上傳檔案按鈕,方法如下。


請在按鈕內增加程式碼,方法如下。
1
2
3
UploadTask task = new UploadTask(ip.getText(), Integer.parseInt(port.getText()), username.getText(), password.getText(), "", filePicker.getSelectedFile());
task.addPropertyChangeListener(this);
task.execute();

發生許多錯誤,沒關係,完整的程式如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
 
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
 
public class FtpClient extends JFrame implements PropertyChangeListener{
 
        private JPanel contentPane;
        private JTextField ip;
        private JTextField port;
        private JTextField username;
        private JTextField password;
        private JFileChooser filePicker;
 
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try {
                                        FtpClient frame = new FtpClient();
                                        frame.setVisible(true);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }
 
        /**
         * Create the frame.
         */
        public FtpClient() {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 531, 585);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(null);
                 
                ip = new JTextField();
                ip.setBounds(86, 10, 419, 33);
                contentPane.add(ip);
                ip.setColumns(10);
                 
                port = new JTextField();
                port.setColumns(10);
                port.setBounds(86, 55, 419, 33);
                contentPane.add(port);
                 
                username = new JTextField();
                username.setColumns(10);
                username.setBounds(86, 96, 419, 33);
                contentPane.add(username);
                 
                password = new JTextField();
                password.setColumns(10);
                password.setBounds(86, 139, 419, 33);
                contentPane.add(password);
                 
                JLabel lblIp = new JLabel("IP\u4F4D\u5740");
                lblIp.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
                lblIp.setBounds(14, 11, 73, 35);
                contentPane.add(lblIp);
                 
                JLabel lblIp_1 = new JLabel("\u5E33\u865F");
                lblIp_1.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
                lblIp_1.setBounds(13, 97, 73, 35);
                contentPane.add(lblIp_1);
                 
                JLabel lblIp_2 = new JLabel("\u5BC6\u78BC");
                lblIp_2.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
                lblIp_2.setBounds(13, 140, 73, 35);
                contentPane.add(lblIp_2);
                 
                JLabel lblIp_3 = new JLabel("\u9023\u63A5\u57E0");
                lblIp_3.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
                lblIp_3.setBounds(11, 57, 73, 35);
                contentPane.add(lblIp_3);
                 
                filePicker = new JFileChooser();
                filePicker.setBounds(10, 180, 495,314);
                contentPane.add(filePicker);
                 
                JButton btnNewButton = new JButton("\u4E0A\u50B3\u6A94\u6848");
                btnNewButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {                           
                                UploadTask task = new UploadTask(ip.getText(), Integer.parseInt(port.getText()), username.getText(), password.getText(), "", filePicker.getSelectedFile());
                                task.addPropertyChangeListener(this);
                                task.execute();
                        }
                });
                btnNewButton.setFont(new Font("微軟正黑體", Font.PLAIN, 18));
                btnNewButton.setBounds(9, 504, 492, 32);
                contentPane.add(btnNewButton);
        }
 
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
                // TODO Auto-generated method stub
                 
        }
}


沒有留言:

張貼留言