FTP客户端的设计实现代码(精编)_第1页
FTP客户端的设计实现代码(精编)_第2页
FTP客户端的设计实现代码(精编)_第3页
FTP客户端的设计实现代码(精编)_第4页
FTP客户端的设计实现代码(精编)_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、import javax.swing.*;import java.awt.Rectangle;import java.io.*;import .*;import java.util.*;public class ccFtp extends JFrame private static final long serialVersionUID = 1L;private JPanel jContentPane = null;private JButton jButtonLocalRefresh = null;private JButton jButtonLocalMKDIR = null;privat

2、e JButton jButtonLocalRename = null;private JButton jButtonLocalDelete = null;private JButton jButtonServerRefresh = null;private JButton jButtonServerMKDIR = null;private JButton jButtonServerRename = null;private JButton jButtonServerDelete = null;private JScrollPane jScrollPaneLocal = null;privat

3、e JScrollPane jScrollPaneServer = null;private JButton jButtonUpload = null;private JButton jButtonDownload = null;private JLabel jLabelSeverAdd = null;private JLabel jLabelPort = null;private JLabel jLabelUserName = null;private JLabel jLabelPassword = null;private JTextField jTextFieldServerAdd =

4、null;private JTextField jTextFieldPort = null;private JTextField jTextFieldUserName = null;private JTextField jTextFieldPassword = null;private JButton jButtonConnect = null;private JButton jButtonDisconnect = null;private JButton jButtonExit = null;private JScrollPane jScrollPaneTextArea = null;pri

5、vate JList jListLocalFile = null;private DefaultListModel listLocalFile;private JList jListServerFile = null;private DefaultListModel listServerFile;private JTextArea jTextAreaStatus = null;private JProgressBar jProgressBar = null;private JScrollPane jScrollPaneDownload = null;private JList jListDow

6、nloadMonitor = null;private DefaultListModel listitemThing; private BufferedReader reader = null; / jve:decl-index=0: private BufferedWriter writer = null;private JRadioButton jRadioButtonPORT = null;private JRadioButton jRadioButtonPASV = null;private JLabel jLabelMode = null;long clickTime = 0;Str

7、ing getPASVip;String ip = null; / jve:decl-index=0: int port = -1; String cmdline; String jListDriveName = D:; / jve:decl-index=0:private JButton getJButtonLocalRefresh() /刷新按钮的功能if (jButtonLocalRefresh = null) jButtonLocalRefresh = new JButton();jButtonLocalRefresh.setBounds(new Rectangle(8, 13, 85

8、, 26);jButtonLocalRefresh.setText(刷新);jButtonLocalRefresh.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) listLocalFile.clear();ListFile(););return jButtonLocalRefresh;private JButton getJButtonLocalMKDIR() /新建文件夹的功能if (jButtonLocalMKDI

9、R = null) jButtonLocalMKDIR = new JButton();jButtonLocalMKDIR.setBounds(new Rectangle(8, 46, 85, 26);jButtonLocalMKDIR.setText(新建文件夹);jButtonLocalMKDIR.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String FloderName = JOptionPane.show

10、InputDialog(New Floder Name:);if(FloderName!=null)File file = new File(jListDriveName+FloderName);file.mkdir();listLocalFile.clear();ListFile(););return jButtonLocalMKDIR;private JButton getJButtonLocalRename() if (jButtonLocalRename = null) jButtonLocalRename = new JButton();jButtonLocalRename.setB

11、ounds(new Rectangle(8, 78, 85, 26);jButtonLocalRename.setText(重命名);jButtonLocalRename.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String oldname=jListLocalFile.getSelectedValue().toString();if(oldname.substring(oldname.length()-1).e

12、qualsIgnoreCase()oldname=oldname.substring(0,oldname.length()-1);File oldf = new File(jListDriveName+oldname);String newname = JOptionPane.showInputDialog(New Name:);if(newname!=null)File newf = new File(jListDriveName+newname);oldf.renameTo(newf);listLocalFile.clear();ListFile(););return jButtonLoc

13、alRename;private JButton getJButtonLocalDelete() if (jButtonLocalDelete = null) jButtonLocalDelete = new JButton();jButtonLocalDelete.setBounds(new Rectangle(8, 111, 85, 26);jButtonLocalDelete.setText(删除);jButtonLocalDelete.addActionListener(new java.awt.event.ActionListener() public void actionPerf

14、ormed(java.awt.event.ActionEvent e) String name=jListLocalFile.getSelectedValue().toString();if(name.substring(name.length()-1).equalsIgnoreCase()name=name.substring(0,name.length()-1);File del = new File(jListDriveName+name);del.delete();listLocalFile.clear();ListFile(););return jButtonLocalDelete;

15、private JButton getJButtonServerRefresh() if (jButtonServerRefresh = null) jButtonServerRefresh = new JButton();jButtonServerRefresh.setBounds(new Rectangle(628, 13, 82, 26);jButtonServerRefresh.setText(刷新);jButtonServerRefresh.addActionListener(new java.awt.event.ActionListener() public void action

16、Performed(java.awt.event.ActionEvent e) try lsCurrentDir(); catch (Exception e1) e1.printStackTrace(););return jButtonServerRefresh;private JButton getJButtonServerMKDIR() if (jButtonServerMKDIR = null) jButtonServerMKDIR = new JButton();jButtonServerMKDIR.setBounds(new Rectangle(628, 46, 82, 26);jB

17、uttonServerMKDIR.setText(新建目录);jButtonServerMKDIR.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String FloderName = JOptionPane.showInputDialog(New Floder Name:);if(FloderName!=null)try writer.write(MKD +FloderName+rn);writer.flush();

18、 cmdline = reader.readLine(); jTextAreaStatus.append(cmdline+n);lsCurrentDir(); catch (Exception e1) e1.printStackTrace(););return jButtonServerMKDIR;private JButton getJButtonServerRename() if (jButtonServerRename = null) jButtonServerRename = new JButton();jButtonServerRename.setBounds(new Rectang

19、le(628, 78, 82, 26);jButtonServerRename.setText(删除文件);jButtonServerRename.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) );return jButtonServerRename;private JButton getJButtonServerDelete() if (jButtonServerDelete = null) jButtonServe

20、rDelete = new JButton();jButtonServerDelete.setBounds(new Rectangle(628, 111, 82, 26);jButtonServerDelete.setText(删除目录);return jButtonServerDelete;private JScrollPane getJScrollPaneLocal() if (jScrollPaneLocal = null) jScrollPaneLocal = new JScrollPane();jScrollPaneLocal.setBounds(new Rectangle(104,

21、 13, 228, 320);jScrollPaneLocal.setViewportView(getJListLocalFile();return jScrollPaneLocal;private JScrollPane getJScrollPaneServer() if (jScrollPaneServer = null) jScrollPaneServer = new JScrollPane();jScrollPaneServer.setBounds(new Rectangle(391, 13, 229, 319);jScrollPaneServer.setViewportView(ge

22、tJListServerFile();return jScrollPaneServer;private JButton getJButtonUpload() if (jButtonUpload = null) jButtonUpload = new JButton();jButtonUpload.setBounds(new Rectangle(340, 115, 60, 30);jButtonUpload.setText(上传);jButtonUpload.addActionListener(new java.awt.event.ActionListener() public void act

23、ionPerformed(java.awt.event.ActionEvent e) UploadArray(););return jButtonUpload;protected void DownloadArray() String Lindex,Rfile;int Lfliename = jListServerFile.getSelectedValue().toString().length();Lindex = jListServerFile.getSelectedValue().toString().substring(Lfliename-1);Rfile = jListServerF

24、ile.getSelectedValue().toString().substring(0,Lfliename-1);if(Lindex.equalsIgnoreCase()JOptionPane.showMessageDialog(null, This is not a file.Connot be Uploaded!, ERROR, JOptionPane.ERROR_MESSAGE);elselistitemThing.insertElementAt(Dload.+Rfile,jListDownloadMonitor.getLastVisibleIndex()+1);jListDownl

25、oadMonitor.setSelectedIndex(jListDownloadMonitor.getFirstVisibleIndex();private JButton getJButtonDownload() if (jButtonDownload = null) jButtonDownload = new JButton();jButtonDownload.setBounds(new Rectangle(340, 156, 60, 30);jButtonDownload.setText(下载);jButtonDownload.addActionListener(new java.aw

26、t.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) DownloadArray(););return jButtonDownload;protected void UploadArray() File directory = new File(jListDriveName+jListLocalFile.getSelectedValue();if(directory.isFile()listitemThing.insertElementAt(Uload.+jListLocalFile

27、.getSelectedValue(),jListDownloadMonitor.getLastVisibleIndex()+1);jListDownloadMonitor.setSelectedIndex(jListDownloadMonitor.getFirstVisibleIndex();elseJOptionPane.showMessageDialog(null, This is not a File!, ERROR, JOptionPane.ERROR_MESSAGE);private JTextField getJTextFieldServerAdd() if (jTextFiel

28、dServerAdd = null) jTextFieldServerAdd = new JTextField();jTextFieldServerAdd.setBounds(new Rectangle(121, 339, 119, 20);jTextFieldServerAdd.setText();return jTextFieldServerAdd;private JTextField getJTextFieldPort() if (jTextFieldPort = null) jTextFieldPort = new JTextField();jTextFieldPor

29、t.setBounds(new Rectangle(121, 362, 119, 20);jTextFieldPort.setText(21);return jTextFieldPort;private JTextField getJTextFieldUserName() if (jTextFieldUserName = null) jTextFieldUserName = new JTextField();jTextFieldUserName.setBounds(new Rectangle(330, 339, 119, 20);jTextFieldUserName.setText(admin

30、);return jTextFieldUserName;private JTextField getJTextFieldPassword() if (jTextFieldPassword = null) jTextFieldPassword = new JTextField();jTextFieldPassword.setBounds(new Rectangle(330, 362, 119, 20);jTextFieldPassword.setText(123456);return jTextFieldPassword;private JButton getJButtonConnect() i

31、f (jButtonConnect = null) jButtonConnect = new JButton();jButtonConnect.setBounds(new Rectangle(452, 339, 109, 42);jButtonConnect.setText(连接);jButtonConnect.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) jTextAreaStatus.append(Start Co

32、nnecting+n);String strServerIP=jTextFieldServerAdd.getText(); String strServerPort=jTextFieldPort.getText(); String strUserName=jTextFieldUserName.getText(); String strPassword=jTextFieldPassword.getText(); try if(strUserName.equals() & strPassword.equals() strUserName=anonymous; strPassword=anonymo

33、us; int iPort=Integer.parseInt(strServerPort); connect(strServerIP,iPort,strUserName,strPassword); catch(Exception e1) jTextAreaStatus.append(Couldnt login serverdont know what happen+n); );return jButtonConnect;protected void connect(String strServerIP, int port, String strUserName, String strPassw

34、ord) trySocket Connectsocket = new Socket(strServerIP,port);reader = new BufferedReader(new InputStreamReader(Connectsocket.getInputStream(); writer = new BufferedWriter(new OutputStreamWriter(Connectsocket.getOutputStream(); writer.write(USER +strUserName+rn); writer.flush(); cmdline = reader.readL

35、ine(); jTextAreaStatus.append(cmdline+n); writer.write(PASS +strPassword+rn); writer.flush(); cmdline = reader.readLine(); jTextAreaStatus.append(cmdline+n); System.out.println(Welcome:+reader.readLine(); lsCurrentDir();catch(Exception e)jTextAreaStatus.append(Cannot be login+n);private void lsCurre

36、ntDir() if(jRadioButtonPORT.isSelected()try Socket dataSocket = dataConnection(LIST);BufferedInputStream dataInput = new BufferedInputStream(dataSocket.getInputStream();listServerFile.clear();int p0=0,p1;byte buff = new byte128;String strget,strtemp = ;while(dataInput.read(buff)0)strget = new String

37、(buff);strtemp+=strget;while(p1=strtemp.indexOf(n,p0)!=-1)if(strtemp.substring(p0,p1).substring(0,1).equalsIgnoreCase(d)listServerFile.addElement(strtemp.substring(p0,p1).substring(55)+);elselistServerFile.addElement(strtemp.substring(p0,p1).substring(55);p0=p1+1;dataInput.close();dataSocket.close()

38、; catch (Exception e) e.printStackTrace();if(jRadioButtonPASV.isSelected()try setPasvMode();Socket clientSocket = new Socket(ip,port);BufferedInputStream dataInput = new BufferedInputStream(clientSocket.getInputStream() ;writer.write(LIST+rn); writer.flush(); cmdline = reader.readLine(); jTextAreaSt

39、atus.append(cmdline+n); listServerFile.clear();int p0=0,p1;byte buff = new byte128;String strget,strtemp = ;while(dataInput.read(buff)0)strget = new String(buff);strtemp+=strget;while(p1=strtemp.indexOf(n,p0)!=-1)if(strtemp.substring(p0,p1).substring(0,1).equalsIgnoreCase(d)listServerFile.addElement

40、(strtemp.substring(p0,p1).substring(55)+);elselistServerFile.addElement(strtemp.substring(p0,p1).substring(55);p0=p1+1;dataInput.close();clientSocket.close();catch (Exception e) e.printStackTrace();private void setPasvMode() try writer.write(PASV+rn);writer.flush(); cmdline = reader.readLine(); jTex

41、tAreaStatus.append(cmdline+n); catch (IOException e1) e1.printStackTrace(); int opening = cmdline.indexOf(); int closing = cmdline.indexOf(), opening + 1); if (closing 0) String dataLink = cmdline.substring(opening + 1, closing); StringTokenizer tokenizer = new StringTokenizer(dataLink, ,); try ip =

42、 tokenizer.nextToken() + . + tokenizer.nextToken() + . + tokenizer.nextToken() + . + tokenizer.nextToken(); port = Integer.parseInt(tokenizer.nextToken() * 256 + Integer.parseInt(tokenizer.nextToken(); catch (Exception e) jTextAreaStatus.append(Cannot get passive mode Ip & Port+n); private Socket da

43、taConnection(String ctrlcmd) String cmd = PORT ;int i;Socket dataSocket = null ;trybyte address = InetAddress.getLocalHost().getAddress() ;ServerSocket serverDataSocket = new ServerSocket(0,1) ;for(i = 0; i 4; +i)cmd = cmd + (addressi & 0xff) + , ;cmd = cmd + (serverDataSocket.getLocalPort() / 256)

44、& 0xff)+,+(serverDataSocket.getLocalPort() & 0xff) ;writer.write(cmd+rn);writer.flush(); cmdline = reader.readLine(); jTextAreaStatus.append(cmdline+n);writer.write(ctrlcmd+rn);writer.flush(); cmdline = reader.readLine(); jTextAreaStatus.append(cmdline+n);dataSocket = serverDataSocket.accept() ;serv

45、erDataSocket.close() ;catch(Exception e)jTextAreaStatus.append(Socket Error+n);return dataSocket ;private JButton getJButtonDisconnect() if (jButtonDisconnect = null) jButtonDisconnect = new JButton();jButtonDisconnect.setBounds(new Rectangle(571, 339, 132, 20);jButtonDisconnect.setText(断开连接);return

46、 jButtonDisconnect;private JButton getJButtonExit() if (jButtonExit = null) jButtonExit = new JButton();jButtonExit.setBounds(new Rectangle(571, 362, 132, 20);jButtonExit.setText(退出);jButtonExit.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEv

47、ent e) System.exit(0););return jButtonExit;private JScrollPane getJScrollPaneTextArea() if (jScrollPaneTextArea = null) jScrollPaneTextArea = new JScrollPane();jScrollPaneTextArea.setBounds(new Rectangle(17, 386, 440, 172);jScrollPaneTextArea.setViewportView(getJTextAreaStatus();return jScrollPaneTextArea;private JList getJListLocalFile

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论