实例介绍
【实例简介】使用java基于socket开发的FTP客户端和服务器,实现了基本的登录,注册,上传下载,等各种功能,同时使用了基于工厂的设计模式,具有较好的可扩展性
【实例截图】
【核心代码】
import java.awt.EventQueue; import javax.swing.*; import java.awt.BorderLayout; import javax.swing.border.BevelBorder; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Color; import java.awt.Font; import java.awt.Toolkit; import javax.swing.table.DefaultTableModel; import javax.swing.border.CompoundBorder; import javax.swing.border.LineBorder; import javax.swing.filechooser.FileSystemView; import org.apache.commons.net.ftp.FTPFile; import java.awt.ScrollPane; import java.awt.Label; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.Vector; import java.awt.Scrollbar; public class Frame_Main implements ActionListener{ //初始化参数-------------------------------- static FTPFile[] file; static String FTP="127.0.0.1"; static String username="liyz"; static String password="000000"; //初始化参数-------------------------------- private JFrame frame; private JTable table; static Ftp_by_me_active ftp; public static Ftp_by_me_active getFtp() { return ftp; } public static FTPFile[] getFile(){ return file; } /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Frame_Main window = new Frame_Main(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Frame_Main() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Frame_Main.class.getResource("/com/sun/java/swing/plaf/windows/icons/UpFolder.gif"))); frame.setTitle("FTP Client"); frame.setBounds(100, 100, 470, 534); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); //显示基本信息(FTP username)----------------------------------------------- JLabel lblNewLabel = new JLabel("FTP IP地址"); lblNewLabel.setBounds(32, 8, 70, 15); frame.getContentPane().add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("用户名"); lblNewLabel_1.setBounds(32, 25, 70, 15); frame.getContentPane().add(lblNewLabel_1); JLabel lblNewLabel_2 = new JLabel("密码"); lblNewLabel_2.setBounds(32, 40, 70, 15); frame.getContentPane().add(lblNewLabel_2); JTextField url = new JTextField("127.0.0.1"); //FTP服务地址 url.setBounds(110,8,82,15); frame.getContentPane().add(url); JTextField usernameField = new JTextField("admin"); //用户名 usernameField.setBounds(110,25,82,15); frame.getContentPane().add(usernameField); JPasswordField passwordField = new JPasswordField("000000"); //密码 passwordField.setBounds(110,40,82,15); frame.getContentPane().add(passwordField); //登录按钮------------------------------------------------ JButton login=new JButton("登录"); login.setFont(new Font("宋体", Font.PLAIN, 12)); login.setBackground(UIManager.getColor("Button.highlight")); login.setBounds(210, 10, 82, 23); frame.getContentPane().add(login); login.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("登录=============="); try { FTP=url.getText().trim(); username=usernameField.getText().trim(); password=passwordField.getText().trim(); ftp=new Ftp_by_me_active(FTP,username,password); if(Ftp_by_me_active.isLogined) { file=ftp.getAllFile(); setTableInfo();//显示所有文件信息 url.setEditable(false); usernameField.setEditable(false); passwordField.setEditable(false); } } catch (Exception e1) { e1.printStackTrace(); JOptionPane.showConfirmDialog(null, "用户名或者密码错误\n username:" username, "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE); } } }); //登录按钮------------------------------------------------ JButton regist=new JButton("注册"); regist.setFont(new Font("宋体", Font.PLAIN, 12)); regist.setBackground(UIManager.getColor("Button.highlight")); regist.setBounds(210, 40, 82, 23); frame.getContentPane().add(regist); regist.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("注册=============="); try { username=usernameField.getText().trim(); password=passwordField.getText().trim(); ftp=new Ftp_by_me_active(FTP,username,password,"register"); if(Ftp_by_me_active.isLogined) { file=ftp.getAllFile(); setTableInfo();//显示所有文件信息 url.setEditable(false); usernameField.setEditable(false); passwordField.setEditable(false); } } catch (Exception e1) { e1.printStackTrace(); JOptionPane.showConfirmDialog(null, "用户名或者密码错误\n username:" username, "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE); } } }); //上传按钮-------------------------------------------------- JButton upload = new JButton("上传"); upload.setFont(new Font("宋体", Font.PLAIN, 12)); upload.setBackground(UIManager.getColor("Button.highlight")); upload.setBounds(312, 40, 82, 23); frame.getContentPane().add(upload); upload.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //上传点击按钮触发------------------------------------ System.out.println("上传!!!!!"); int result = 0; File file = null; String path = null; JFileChooser fileChooser = new JFileChooser(); FileSystemView fsv = FileSystemView.getFileSystemView(); System.out.println(fsv.getHomeDirectory()); //得到桌面路径 fileChooser.setCurrentDirectory(fsv.getHomeDirectory()); fileChooser.setDialogTitle("请选择要上传的文件..."); fileChooser.setApproveButtonText("确定"); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); result = fileChooser.showOpenDialog(null); if (JFileChooser.APPROVE_OPTION == result) { path=fileChooser.getSelectedFile().getPath(); System.out.println("path: " path); try { //下载 ftp.upload(path); System.out.println("文件上传成功"); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } //上传点击按钮触发------------------------------------ } }); //上传按钮-------------------------------------------------- //刷新按钮-------------------------------------------------- JButton refresh = new JButton("刷新"); refresh.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try{ file=ftp.getAllFile(); setTableInfo(); } catch (Exception e) { e.printStackTrace(); } } }); refresh.setFont(new Font("宋体", Font.PLAIN, 12)); refresh.setBackground(UIManager.getColor("Button.highlight")); refresh.setBounds(312, 10, 82, 23); frame.getContentPane().add(refresh); //刷新按钮-------------------------------------------------- } //显示基本信息----------------------------------------------- private void setTableInfo() { //table数据初始化 从FTP读取所有文件 String[][] data1=new String[file.length][4]; for(int row=0;row<file.length;row ) { data1[row][0]=file[row].getName(); if(file[row].isDirectory()) { data1[row][1]="文件夹"; } else if(file[row].isFile()){ String[] geshi=file[row].getName().split("\\."); data1[row][1]=geshi[1]; } data1[row][2]=file[row].getSize() ""; data1[row][3]="下载"; } //table列名----------------------------------------------------- String[] columnNames = {"文件", "文件类型", "文件大小(B)", "" }; DefaultTableModel model = new DefaultTableModel(); model.setDataVector(data1, columnNames); //加滚动条-------------------------------------------------------- JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(32, 73, 400, 384); frame.getContentPane().add(scrollPane); //加滚动条----------------------------------------------------- //table功能------------------------------------------------------ table = new JTable(model); scrollPane.setViewportView(table); table.setColumnSelectionAllowed(true); table.setCellSelectionEnabled(true); table.setFont(new Font("微软雅黑", Font.PLAIN, 12)); table.setBorder(new LineBorder(new Color(0, 0, 0))); table.getColumnModel().getColumn(0).setPreferredWidth(200); table.setToolTipText("可以点击下载"); //table button初始化(最后一列的按键)-------------------- ButtonColumn buttonsColumn = new ButtonColumn(table, 3); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论