lsdwyl 发表于 2015-5-27 10:14:17

简单的FTP上传下载(java实现 swing界面)

  /**
*阅读前请自己在win7上建立FTP主机
*具体步骤如:http://jingyan.baidu.com/article/574c5219d466c36c8d9dc138.html
* 然后将以下FTP,username,password分别改成你的FTP ip地址 用户名 密码即可(红色代码部分)
* 本例子用了apche的commons-net-3.3.jar以方便FTP的访问 请在网上下载然后手动buid -path添加
* 待完成版刷新按钮 登录 都还没有做 而且上传 下载 完成后都需要重新运行
* 2014-05-07
* **/
  
  一共3个类 Frame_Main为主函数 图形界面ButtonColumn是下载键的类 最后一个是FTP服务类

  



1 import java.awt.EventQueue;
2
3 import javax.swing.JFrame;
4 import java.awt.BorderLayout;
5 import javax.swing.JTable;
6 import javax.swing.border.BevelBorder;
7 import javax.swing.JFileChooser;
8 import javax.swing.JScrollPane;
9 import javax.swing.JTextField;
10 import javax.swing.JButton;
11 import javax.swing.JRadioButton;
12 import javax.swing.JTextArea;
13 import javax.swing.JLabel;
14 import java.awt.event.ActionListener;
15 import java.awt.event.ActionEvent;
16 import java.awt.Color;
17 import java.awt.Font;
18 import javax.swing.SwingConstants;
19 import javax.swing.UIManager;
20 import java.awt.Toolkit;
21 import javax.swing.table.DefaultTableModel;
22 import javax.swing.border.CompoundBorder;
23 import javax.swing.border.LineBorder;
24 import javax.swing.filechooser.FileSystemView;
25 import javax.swing.JScrollBar;
26
27 import org.apache.commons.net.ftp.FTPFile;
28
29 import java.awt.ScrollPane;
30 import java.awt.Label;
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.Vector;
36 import java.awt.Scrollbar;
37
38 public class Frame_Main implements ActionListener{
39
40   
41   //初始化参数--------------------------------
42         static FTPFile[] file;
43         static String FTP="192.168.1.86";
44         static String username="huanglizhe";
45         static String password="123456";
46   //初始化参数--------------------------------
47   
48   
49   private JFrame frame;
50   private JTable table;
51   static Ftp_by_apache ftp;
52   public static Ftp_by_apache getFtp() {
53         return ftp;
54   }
55   
56   /**
57      * Launch the application.
58      */
59   public static void main(String[] args) {
60         
61          ftp=new Ftp_by_apache(FTP,username,password);
62          file=ftp.getAllFile();
63         
64         
65         
66         EventQueue.invokeLater(new Runnable() {
67             public void run() {
68               try {
69                     Frame_Main window = new Frame_Main();
70                     window.frame.setVisible(true);
71               } catch (Exception e) {
72                     e.printStackTrace();
73               }
74             }
75         });
76         
77   }
78
79   /**
80      * Create the application.
81      */
82   public Frame_Main() {
83         initialize();
84   }
85
86   /**
87      * Initialize the contents of the frame.
88      */
89   private void initialize() {
90         frame = new JFrame();
91         frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Frame_Main.class.getResource("/com/sun/java/swing/plaf/windows/icons/UpFolder.gif")));
92         frame.setTitle("FTP");
93         frame.setBounds(100, 100, 470, 534);
94         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
95         frame.getContentPane().setLayout(null);
96         
97         //上传按钮--------------------------------------------------
98         JButton upload = new JButton("\u4E0A\u4F20");
99         upload.setFont(new Font("宋体", Font.PLAIN, 12));
100         upload.setBackground(UIManager.getColor("Button.highlight"));
101         upload.addActionListener(new ActionListener() {
102             public void actionPerformed(ActionEvent arg0) {
103               //上传点击按钮触发------------------------------------
104               System.out.println("上传!!!!!");
105               int result = 0;
106               File file = null;
107               String path = null;
108               JFileChooser fileChooser = new JFileChooser();
109               FileSystemView fsv = FileSystemView.getFileSystemView();
110               System.out.println(fsv.getHomeDirectory());                //得到桌面路径
111               fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
112               fileChooser.setDialogTitle("请选择要上传的文件...");
113               fileChooser.setApproveButtonText("确定");
114               fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
115               result = fileChooser.showOpenDialog(null);
116               if (JFileChooser.APPROVE_OPTION == result) {
117                     path=fileChooser.getSelectedFile().getPath();
118                     System.out.println("path: "+path);
119                     try {
120                         //下载
121                         ftp.upload(path);
122                     } catch (IOException e1) {
123                         // TODO Auto-generated catch block
124                         e1.printStackTrace();
125                     }
126                     finally{
127                        
128                         ftp.close_connection();
129                     }
130                     }
131               //上传点击按钮触发------------------------------------
132             }
133         });
134         upload.setBounds(195, 15, 82, 23);
135         frame.getContentPane().add(upload);
136         //上传按钮--------------------------------------------------
137         
138         
139         
140         //刷新按钮--------------------------------------------------
141         JButton refresh = new JButton("\u5237\u65B0");
142         refresh.addActionListener(new ActionListener() {
143             public void actionPerformed(ActionEvent arg0) {
144             }
145         });
146         refresh.setFont(new Font("宋体", Font.PLAIN, 12));
147         refresh.setBackground(UIManager.getColor("Button.highlight"));
148         refresh.setBounds(312, 15, 82, 23);
149         frame.getContentPane().add(refresh);
150         //刷新按钮--------------------------------------------------
151         
152         
153         
154         //显示基本信息(FTP username)-----------------------------------------------
155         JLabel lblNewLabel = new JLabel("FTP\u5730\u5740");
156         lblNewLabel.setBounds(32, 10, 54, 15);
157         frame.getContentPane().add(lblNewLabel);
158         
159         JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D");
160         lblNewLabel_1.setBounds(32, 35, 54, 15);
161         frame.getContentPane().add(lblNewLabel_1);
162         
163         JLabel address = new JLabel(FTP);
164         address.setBounds(110, 10, 75, 15);
165         frame.getContentPane().add(address);
166         
167         JLabel name = new JLabel(username);
168         name.setBounds(110, 35, 82, 15);
169         frame.getContentPane().add(name);
170         //显示基本信息-----------------------------------------------
171         
172         
173         //table数据初始化从FTP读取所有文件
174         String[][] data1=new String;
175          for(int row=0;row
页: [1]
查看完整版本: 简单的FTP上传下载(java实现 swing界面)