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

📄 dirclientapplet.java

📁 java编程开发技巧与实例的编译测试通过的所有例程
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.text.DecimalFormat;

public class DirClientApplet extends Applet implements ActionListener
{
	private Socket connection	=	null;
	private DataOutputStream out	=	null;
	private ObjectInputStream in	=	null;
	private List dirList	=	new List();
	private List fileList	=	new List();
	private TextField passwd	=	new TextField(10);
	private Label status	=	new Label("Click 'Connect' . . . ");
	private Button connect	=	new Button("Connect");
	private Button disconnect	=	new Button("Disconnect");
	private boolean connected	=	false;
	private DecimalFormat formatTool	=	new DecimalFormat("#.###");
	private String host	=	"localhost";
	
	public void init()
	{
		if (getParameter("host") != null)
			host = getParameter("host");
		Panel buttons	=	new Panel(new FlowLayout());
		buttons.add(new Label("Password: "));
		buttons.add(passwd);
		buttons.add(connect);
		buttons.add(disconnect);
		passwd.setEchoChar('*');
		Panel lists	=	new Panel(new BorderLayout());
		lists.add("North", new PanelBox(dirList, "Directories"));
		lists.add("Center", new PanelBox(fileList, "Files"));
		setLayout(new BorderLayout());
		add("North", new PanelBox(status, "Status"));
		add("Center", lists);
		add("South", buttons);
		connect.addActionListener(this);
		disconnect.addActionListener(this);
		dirList.addActionListener(this);
		setSize(300, 300);
		///setLocationRelativeTo(null);
	}
	private void send(String cmd, String option) throws IOException
	{
		out.writeUTF(cmd);
		out.writeUTF(option);
		out.flush();
	}
	private void connect()
	{
		try
		{
			connection	=	new Socket(host, 8765);
			out	=	new DataOutputStream(connection.getOutputStream());
			in	=	new ObjectInputStream(connection.getInputStream());
			send("pass", "dirserver 1.0 |" + passwd.getText());
			if (in.readUTF().equals("okay"))
			{
				status.setText("Authenticaing  . . . connected");
				connected	=	true;
				showDirectory("pwd", "dummy");
			}
			else
				status.setText("Authenticating . . . REFUSED");
		}
		catch	(Exception ex)
		{
			status.setText("Error: " + ex);
		}
	}
	private void showDirectory(String cmd, String path)
	{
		try
		{
			send(cmd, path);
			path = in.readUTF();
			send("list", "dummy");
			FileInfo list[]	=	(FileInfo[])in.readObject();
			dirList.removeAll();
			fileList.removeAll();
			dirList.add("..");
			long total = 0, nDirs = 0, nFiles = 0;
			for (int i = 0; i < list.length; i ++)
			{
				if (list[i].isDirectory())
				{
					nDirs ++;
					dirList.add(list[i].toString());
				}
				else
				{
					nFiles ++;
					total += list[i].length();
					fileList.add(list[i].toString());
				}
			}
			status.setText(path + ", " + nDirs + " dirs, " + nFiles + " file, " +
							formatTool.format(total) + " bytes]");
		}
		catch	(Exception ex)
		{
			status.setText("Error: " + ex);
		}
	}
	public void stop()
	{
		if (connected)
		{
			try
			{
				send("bye", "dummy");
				dirList.removeAll();
				fileList.removeAll();
				in.close();
				out.close();
				connection.close();
			}
			catch	(IOException ioe)
			{
				System.err.println("Error: " + ioe);
			}
			finally
			{
				connected	=	false;
				passwd.setText("");
				status.setText("Disconnected. Click 'Connect' . . . ");
			}
		}
	}
	public void actionPerformed(ActionEvent ae)
	{
		if ((!connected) && (ae.getSource() == connect))
			connect();
		else if (connected && (ae.getSource() == disconnect))
			stop();
		else if (connected && (ae.getSource() == dirList))
			showDirectory("chdir", dirList.getSelectedItem());
	}
}

⌨️ 快捷键说明

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