⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpclientframe.java

📁 实现FTP服务器与客户端的功能,主要解决得了同时上传数据时的冲突问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//package FtpClient;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;


public class FtpClientFrame extends JFrame 
{
	 protected JToolBar toolbar;
	 protected List list;
	 protected JMenuBar menuBar;
	 
	 JMenu menuFile;
	 JMenu menuHelp;
	 JMenuItem menuHelpItem ;
	 JMenuItem menuLog ;
	 public static TextArea textarea;
	 
	 Action lsAction;
	 Action cdAction;
	 Action pwdAction;
	 Action mkdAction;
	 Action getAction;
	 Action putAction;
	 Action ascAction;
	 Action binAction;
	 Action aborAction;
	 Action deleAction;
	 Action quitAction;
	 Action cdupAction;
	 
	 //控制用socket
     Socket ctrlSocket;				//控制用socket
	 public PrintWriter ctrlOutput;	//控制输出的流
	 public BufferedReader ctrlInput;//控制输入的流
	 String username,password,host;
	 final int CTRLPORT = 21 ;		//控制用端口
	 
	 LoginDlg logdlg;
	 HelpDlg helpdlg;
	 MkdDlg mkddlg;
	 FileDialog fileopendlg;
/*****************************************************************************/	
	 // 构造函数
	 public FtpClientFrame() 
	 {
		  // 调用系统外观/java外观
		 try {
		     UIManager.setLookAndFeel(//"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
		   "javax.swing.plaf.metal.MetalLookAndFeel");
		  } catch (Exception e) { }
		
		  //关闭窗口
		   menuBar = new JMenuBar();
		
		  addWindowListener(new WindowAdapter() {
		   	public void windowClosing(WindowEvent e) {
		    	System.exit(0);
		   }
		  });
		  		  
		  //以下是外观组件
		  createActionObjects();
		  createToolbar();
		  createMenuBar();
		  textarea = new TextArea(3,3);
		  textarea.setEditable(false);
		  		  		  
           setTitle("FTP Client");
           setSize(new Dimension(600, 364));	 

		
		  list=new List(20,false);
		  Container cp = getContentPane();
		  JPanel panel = new JPanel();
		  cp.setLayout(new BorderLayout());
		  panel.setLayout(new BorderLayout());		  
		  cp.add(menuBar, BorderLayout.NORTH);
		  cp.add(panel, BorderLayout.CENTER);
		  panel.add(toolbar,BorderLayout.NORTH);
		  panel.add(list,BorderLayout.CENTER);		  
		  panel.add(textarea,BorderLayout.SOUTH);
		  toolbar.setFloatable(true);   //toolbar可以被移动
		  
		  //以下是流
		  host = null;
		  
		  mkddlg = new MkdDlg(this,"输入要创建的文件名或目录名",true);
		  fileopendlg = new FileDialog(this,"打开文件对话框^_^",FileDialog.LOAD);
		  fileopendlg.setVisible(false);
		  fileopendlg.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				fileopendlg.setVisible(false);
			}
		});


		  
		  //Menu 用户登陆 事件
		  logdlg = new LoginDlg(this,"用户登陆",true);
		  menuLog.addActionListener
          (
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   logdlg.setVisible(true);
                   username = logdlg.username;
                   password = logdlg.password;
                   host = logdlg.ip;
                   doLogin();
                   
                }
            }
          ); 

          //Menu 帮助 事件
		  helpdlg = new HelpDlg(this,"说明",true);
		  menuHelpItem.addActionListener
          (
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   helpdlg.setVisible(true);
                }
            }
          ); 
	 }
	
/********************************************************************************/
	 protected void createActionObjects() 
	 {
		  ImageIcon icon;  // 用来加载 toolbar 的 icons
		  
		  // 1 Ls
		  // 取得目录信息
		  icon = new ImageIcon("ls.gif");
		  lsAction = new AbstractAction("ls", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {		    
			  doLs();
		   }
		  };
			//2 cd
		  icon = new ImageIcon("cd.gif");
		  cdAction = new AbstractAction("cd", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   	   doCd();
		   }
		  };
		
		  // 3 pwd
		  icon = new ImageIcon("pwd.gif");
		  pwdAction = new AbstractAction("pwd", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		       doPwd();
		   }
		  };
		
		  // 4 cdup
		  icon = new ImageIcon("cdup.gif");
		  cdupAction = new AbstractAction("cdup", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
				doCdup();
		   }
		  };
		  // 5 mkd
		  icon = new ImageIcon("mkd.gif");
		  mkdAction = new AbstractAction("mkd", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   		mkddlg.setVisible(true);
		   		doMkd();
		   }
		  };
		  // 6 get
		  icon = new ImageIcon("get.gif");
		  getAction = new AbstractAction("get", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   		doGet();
		   }
		  };
		  // 7 put
		  icon = new ImageIcon("put.gif");
		  putAction = new AbstractAction("put", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   		fileopendlg.setVisible(true);
		   		doPut();
		   }
		  };
		  // 8 ASC
		  icon = new ImageIcon("asc.gif");
		  ascAction = new AbstractAction("asc", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   	   doAscii();
		   }
		  };
		  // 9 Bin
		  icon = new ImageIcon("bin.gif");
		  binAction = new AbstractAction("bin", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   	  doBinary();
		   }
		  };
		  // 10 Abor
		  icon = new ImageIcon("abor.gif");
		  aborAction = new AbstractAction("abor", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   		doAbor();
		   }
		  };
		  // 11 dele
		  icon = new ImageIcon("dele.gif");
		  deleAction = new AbstractAction("dele", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   		doDele();
		   }
		  };
		  // 12	quit
		  icon = new ImageIcon("quit.gif");
		  quitAction = new AbstractAction("quit", icon) 
		  {
		   public void actionPerformed(ActionEvent e) 
		   {
		   	 doQuit();
 		   }
 		  };
	 }
/********************************************************************************/
	 // 创建 toolbar
	 protected void createToolbar() 
	 {
		  toolbar = new JToolBar();
		  toolbar.add(lsAction);
		  toolbar.add(cdAction);
		  toolbar.add(pwdAction);
		  
		  toolbar.addSeparator();
		  
		  toolbar.add(mkdAction);
		  toolbar.add(getAction);
		  toolbar.add(putAction);
		  toolbar.addSeparator();
		  toolbar.add(ascAction);
		  toolbar.add(binAction);
		  toolbar.add(aborAction);
		  toolbar.addSeparator();
		  toolbar.add(cdupAction);
		  toolbar.add(deleAction);
		  toolbar.add(quitAction);
	 }
/********************************************************************************/
	 protected void createMenuBar() 
	 {
		   menuFile = new JMenu();
		   menuHelp = new JMenu();
		   menuLog = new JMenuItem();
		   menuHelpItem = new JMenuItem();
		   menuFile.setText("操作");
		   menuHelp.setText("帮助");
		   menuHelpItem.setText("说明");
		   menuLog.setText("登陆FTP");
		   menuFile.add(menuLog);
		   menuHelp.add(menuHelpItem);
		   menuBar.add(menuFile);
		   menuBar.add(menuHelp);
		 
	 }

/********************************************************************************/
		// doLogin方法
		// 登陆FTP 服务器
	 public void doLogin()
	 {
		try{
		    ctrlSocket = new Socket("127.0.0.1", CTRLPORT);//21 host
	        ctrlOutput = new PrintWriter(ctrlSocket.getOutputStream());
		    ctrlInput  = new BufferedReader(new InputStreamReader
							(ctrlSocket.getInputStream()));
							
			CtrlListen listener = new CtrlListen(ctrlInput) ;
			Thread listenerthread = new Thread(listener) ;
			listenerthread.start() ;
							
			ctrlOutput.println("USER " + "yang");//username) ;
			ctrlOutput.flush() ;			//刷新流
			ctrlOutput.println("PASS " + "104");//password) ;
			ctrlOutput.flush() ;
			logdlg.textuser.setEditable(false);
			logdlg.textpass.setEditable(false);
			logdlg.textip.setEditable(false);
			String ctrstr=ctrlInput.readLine();
		//	while(ctrstr!=null)
		//	{
		//		textarea.append(ctrstr+"\n") ;	
		//		ctrstr=ctrlInput.readLine();
		//	}		
		}catch(Exception e)
		{
			e.printStackTrace();
			//System.exit(1);
		}
	}
/********************************************************************************/
	// doLs方法
	// 取得目录信息
	public void doLs()
	{
		try{
			int n ;
			int p0=0,p1;
			byte[] buff = new byte[128] ;
			String strget,strtemp="",strout,strout2;
			char ch;

			// 建立数据连接
			Socket dataSocket = dataConnection("LIST") ;
			// 准备读取数据用流
			BufferedInputStream dataInput
							= new BufferedInputStream(dataSocket.getInputStream()) ;
			list.removeAll();
			// 读取目录信息
			while((n = dataInput.read(buff))>0)
			{
				strget = new String(buff);
				strtemp+=strget;
				System.out.write(buff,0,127) ;//测试,可以把buff中的内容"取出"				
				//for(int i=0;i<=127;i++)
				//{
				//	buff[i]=0;
				//}
				while((p1 = strtemp.indexOf("\n",p0))!=-1)//没有到达串尾
				{								  
					strout = strtemp.substring(p0,p1);//返回子串,0 ~ p-1 */
					p0 = p1+1;
					if(strout.startsWith("\n"))
					{
						for(int i=0;i<strout.length();i++)
						{
							if((ch = strout.charAt(i))!='\r'
								||(ch = strout.charAt(i))!='\n' 
									||(ch = strout.charAt(i))!=' ' 
										||(ch = strout.charAt(i))!='\t')
							{
								p1=i;
								break;
							}
						}
						//p1=strout.lastIndexOf("\n",strout.length()-2);
						strout2=strout.substring(p1);
						list.add(strout2);
					}
					else
					{
						list.add(strout);
					}
				}
				//try{
				//	Thread.sleep(500);
				//}catch(InterruptedException e){}
			}
			dataInput.close();			
			dataSocket.close() ;
		}catch(Exception e)
		{
			e.printStackTrace();
			//System.exit(1);
		}

	}
/********************************************************************************/
	// doCd方法
	// 切换目录
	public void doCd()
	{
		String dirName = "" ;
		try{
			dirName =list.getSelectedItem();
			ctrlOutput.println("CWD " + dirName) ;// CWD命令
			ctrlOutput.flush() ;
			doLs();		//调用doLs()
		}catch(Exception e)
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -