设为首页 收藏本站
查看: 1397|回复: 0

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

[复制链接]

尚未签到

发表于 2015-5-27 10:14:17 | 显示全部楼层 |阅读模式
  /**
*阅读前请自己在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服务类
DSC0000.png
  



  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[file.length][4];
175          for(int row=0;row

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-71146-1-1.html 上篇帖子: ftp.21端口和20端口 下篇帖子: 从FTP下载文件后把数据放在数据库里.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表