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

📄 fss.java

📁 局域网传输共享文件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
				// 接收
				DatagramSocket parentdirserver = new DatagramSocket(9001);
				byte[] b2 = new byte[40960];
				DatagramPacket parentdirserverp = new DatagramPacket(b2,
						b2.length);
				parentdirserver.receive(parentdirserverp);
				parentdirserver.close();
				// 转换成目录
				StringTokenizer stn = new StringTokenizer(new String(
						parentdirserverp.getData(), 0, parentdirserverp
								.getLength()).trim(), "?");
				String first = parentdirname.substring(3, parentdirname
						.length() - 1);
				path.setText(first.substring(0, first.lastIndexOf("\\")));
				if (first.substring(0, first.lastIndexOf("\\")).equals(host)) {
					shua.setEnabled(true);
				}
				rr = new String[stn.countTokens()];
				String[] ss = new String[rr.length];
				for (int i = 0; i < rr.length; i++) {
					rr[i] = stn.nextToken().trim();
					ss[i] = rr[i].substring(0, rr[i].indexOf("{"));
				}
				list = new JList(ss);
				list.addMouseListener(this);
				// lmodel = list.getModel();
				pcenter.add(list);
				pcenter.getViewport().setView(list);
				ToolTipManager.sharedInstance().registerComponent(list);
				this.validate();
			} catch (Exception x) {

			}
		} else if (s.equals("进入本地")) {// 未完全实现
			new EnterBendi().start();
		} else if (s.equals("开始")) {
			// 显示登录对话框
			d = new Dialog(this, "请输入用户名和密码", true);
			item4.setEnabled(false);
			d.setLayout(new GridLayout(4, 1));
			JPanel p4 = new JPanel();
			p4.setLayout(new FlowLayout(FlowLayout.LEFT));
			p4.add(new JLabel("IP地址:"));
			ip2 = new JTextField(15);
			ip2.setSelectedTextColor(Color.BLUE);
			ip2.setText("121.250.218.232");
			ip2.setEditable(true);
			ip2.setBorder(new EtchedBorder(Color.GRAY, Color.LIGHT_GRAY));
			ip2.setBackground(Color.WHITE);
			d.setSize(300, 150);
			Dimension sc = getToolkit().getScreenSize();
			Dimension sz = d.getSize();
			d.setLocation(sc.width / 2 - sz.width / 2, sc.height / 2
					- sz.height / 2);
			p4.add(ip2);
			d.add(p4);
			JPanel p1 = new JPanel();
			p1.setLayout(new FlowLayout(FlowLayout.LEFT));
			p1.add(new JLabel("用户名:"));
			usert = new JTextField(20);
			usert.setBorder(new EtchedBorder(Color.GRAY, Color.LIGHT_GRAY));
			usert.setText("sqk");
			p1.add(usert);
			d.add(p1);
			JPanel p2 = new JPanel();
			p2.setLayout(new FlowLayout(FlowLayout.LEFT));
			p2.add(new JLabel("密码:  "));
			idt = new JPasswordField(20);
			idt.setBorder(new EtchedBorder(Color.GRAY, Color.LIGHT_GRAY));
			idt.setText("1989");
			p2.add(idt);
			d.add(p2);
			JPanel p3 = new JPanel();
			p3.setLayout(new FlowLayout(FlowLayout.CENTER));
			JButton b;
			b = new JButton("确认");
			b.setBounds(10, 10, 10, 10);
			b.addActionListener(this);
			p3.add(b);
			b = new JButton("退出");
			b.addActionListener(this);
			p3.add(b);
			d.add(p3);
			d.show();
		}
	}

	@SuppressWarnings( { "static-access", "deprecation" })
	public FSS() {// 主界面

		setBackground(Color.CYAN);
		users.put("sqk", "1989");
		user.add("sqk");
		users.put("wangshu", "123");
		user.add("wangshu");
		v.add("D:");
		v.add("C:");

		this.setSize(600, 600);
		this.setResizable(false);
		Dimension sc = getToolkit().getScreenSize();
		Dimension sz = getSize();
		setLocation(sc.width / 2 - sz.width / 2, sc.height / 2 - sz.height / 2);
		this.setTitle("FSS");
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		JMenuItem item1, item2, item3;
		menu1.setEnabled(false);
		item1 = new JMenuItem("重命名");
		item1.addActionListener(this);
		menu1.add(item1);
		item1 = new JMenuItem("删除");
		item1.addActionListener(this);
		menu1.add(item1);
		item1 = new JMenuItem("拷贝");
		item1.addActionListener(this);
		menu1.add(item1);

		item1 = new JMenuItem("移动");
		item1.addActionListener(this);
		menu1.add(item1);
		item2 = new JMenuItem("下载的文件");
		item2.addActionListener(this);
		menu2.add(item2);
		item4 = new JMenuItem("QQ,文件传输");
		item4.addActionListener(this);
		menu3.add(item4);
		item4.setEnabled(false);
		item3 = new JMenuItem("时钟");
		item3.addActionListener(this);
		menu3.add(item3);
		JMenuBar bar = new JMenuBar();
		JButton start = new JButton("开始"), transfer = new JButton("进入本地");
		shua = new JButton("刷新");
		tuichu = new JButton("退出登录");
		start.addActionListener(this);
		transfer.addActionListener(this);
		shua.addActionListener(this);
		tuichu.addActionListener(this);
		tuichu.setEnabled(false);
		shua.setEnabled(false);
		bar.add(start);
		bar.add(transfer);
		bar.add(shua);
		bar.add(tuichu);
		item5 = new JMenuItem("确定");
		item5.addActionListener(this);
		menu1.add(item5);
		item5.setEnabled(false);
		bar.add(menu1);
		bar.add(menu2);
		bar.add(menu3);
		setJMenuBar(bar);
		Container content = this.getContentPane();
		JPanel pup = new JPanel();
		pup.setLayout(new GridLayout(3, 1));
		JPanel pup1 = new JPanel();
		JPanel pup2 = new JPanel();
		pup2.setLayout(new FlowLayout(FlowLayout.CENTER));
		pup1.setLayout(new FlowLayout(FlowLayout.CENTER));
		pup1.add(new JLabel("当前路径:"));
		path = new JTextArea(2, 30);
		path.setEditable(false);
		path.setBackground(Color.ORANGE);
		JScrollPane ppath = new JScrollPane(path);
		ppath
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		pup1.add(ppath);
		pup.add(pup1);
		pup2.add(new JLabel("状态:"));
		tip = new JTextField(30);
		tip.setEditable(false);
		tip.setBackground(Color.ORANGE);
		pup2.add(tip);
		pup.add(pup2);
		baaa = new JProgressBar();
		// baaa.setForeground(Color.GREEN);
		JPanel pup3 = new JPanel();
		pup3.setLayout(new FlowLayout(FlowLayout.CENTER));
		pup3.add(baaa);
		baaa.setSize(40, 5);
		baaa.show();
		pup.add(pup3);
		content.add(pup, "North");
		pcenter = new JScrollPane();

		ToolTipManager.sharedInstance().registerComponent(pcenter);// 添加list
		pcenter
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		pcenter
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		content.add(pcenter);
		JPanel pdown = new JPanel();
		pdown.setLayout(new FlowLayout(FlowLayout.CENTER));
		b2 = new JButton("上级目录");
		b2.addActionListener(this);
		b2.setEnabled(false);
		pdown.add(b2);
		pdown.add(new JLabel("IP:"));
		ip = new JTextField(16);
		ip.setText("      (空)");
		ip.setEditable(false);
		ip.setEnabled(false);
		pdown.add(ip);
		content.add(pdown, "South");
		this.setVisible(true);
		new JieshouLogIn().start();
		new FasongMoren().start();
		new link().start();
		new fasongdir().start();
		new jieshoudelete().start();
		new JieshouDownload().start();
	}

	private class LogIn extends Thread {// 发送登录验证并接受验证结果
		private String logs;

		private String logi;

		public LogIn(String s1, String i1) {
			logs = s1;
			logi = i1;
		}

		public void run() {
			InetAddress add;
			DatagramSocket socout = null;
			try {
				byte[] loginbyte = (logs + "\n" + logi).getBytes();
				add = InetAddress.getByName(ip2.getText().trim());
				socout = new DatagramSocket();// add, 6000
				DatagramPacket socoutp = new DatagramPacket(loginbyte,
						loginbyte.length, add, 6000);
				socout.send(socoutp);
				socout.close();
				DatagramSocket socin = new DatagramSocket(6007);
				byte[] loginbyte2 = new byte[20];
				DatagramPacket socinp = new DatagramPacket(loginbyte2,
						loginbyte2.length);
				socin.receive(socinp);
				String logs = new String(socinp.getData(), 0, socinp
						.getLength()).trim();
				pass = Integer.parseInt(logs.substring(0, 1));
				host = logs.substring(1);
				socin.close();
			} catch (Exception e) {
				if ((socout != null) && !(socout.isClosed())) {
					socout.close();
				}
			}
		}
	}

	private class JieshouLogIn extends Thread {// 随时接受要登录的用户的验证信息并返回验证结果
		public void run() {
			InetAddress add;
			while (true) {
				try {
					byte[] JieshouLogInbyte = new byte[1024];
					DatagramPacket JieshouLogInsp = new DatagramPacket(
							JieshouLogInbyte, JieshouLogInbyte.length);
					DatagramSocket JieshouLogIns = new DatagramSocket(6000);
					JieshouLogIns.receive(JieshouLogInsp);
					add = JieshouLogInsp.getAddress();
					String host3 = add.getHostName() + "         {"
							+ add.toString() + "}";
					JieshouLogIns.close();
					String jieshous = new String(JieshouLogInsp.getData(), 0,
							JieshouLogInsp.getLength());
					int jieshousi = jieshous.indexOf("\n");
					String user1 = jieshous.substring(0, jieshousi).trim();
					String passwd1 = jieshous.substring(jieshousi + 1).trim();
					boolean found = false;
					if (passwd1.equals((String) users.get(user1))) {
						found = true;
						zaixian.addElement(host3);
					}
					int pass2 = 0;
					if (found) {
						pass2 = 1;
					}
					byte[] JieshouLogInbyte2 = (Integer.toString(pass2) + InetAddress
							.getLocalHost().getHostName()).getBytes();
					host2 = InetAddress.getLocalHost().getHostName();
					DatagramSocket jieshousoc = new DatagramSocket();// add,
					// 6007
					DatagramPacket jieshousocp = new DatagramPacket(
							JieshouLogInbyte2, JieshouLogInbyte2.length, add,
							6007);
					jieshousoc.send(jieshousocp);
					jieshousoc.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	private class FasongMoren extends Thread {// 发送初始目录
		public void run() {
			InetAddress add;
			DatagramSocket start1 = null;
			while (true) {
				try {
					start1 = new DatagramSocket(7009);

					byte[] startb = new byte[5];
					DatagramPacket start1p = new DatagramPacket(startb,
							startb.length);
					start1.receive(start1p);
					add = start1p.getAddress();
					String starts = new String(start1p.getData(), 0, start1p
							.getLength()).trim();
					start1.close();

					String s0 = "";
					if (starts.equals("YES")) {
						for (int j = 0; j < v.size(); j++) {
							String s = "?" + (String) v.elementAt(j) + "{";
							File f = new File((String) v.elementAt(j));
							int count = 0;
							if (write.get(f.getAbsolutePath()) == null) {
								s += "W";
								count++;
							}
							if (read.get(f.getAbsolutePath()) == null) {
								s += "R";
								count++;
							}
							if (exe.get(f.getAbsolutePath()) == null) {
								s += "X";
								count++;
							}
							if (!notdeletef.contains(f.getAbsolutePath())) {
								s += "D";
								count++;
							}
							if (f.exists()) {
								s += "}" + printspaces(2 - count) + "    "
										+ f.length();
							}
							if (f.isDirectory()) {
								s += "#";
							}
							s0 += s;
						}
					}
					DatagramSocket fasongs = new DatagramSocket();// add, 7001
					byte[] fasongb = s0.getBytes();
					DatagramPacket fasongsp = new DatagramPacket(fasongb,
							fasongb.length, add, 7001);
					fasongs.send(fasongsp);
					fasongs.close();
				} catch (Exception e) {
					if (!start1.isClosed())
						start1.close();
					e.printStackTrace();
				}
			}
		}
	}

	private class EnterBendi extends Thread {// 启动进入本地线程
		public void run() {
			new BendiFrame();
		}
	}

	private String printspaces(int k) {
		String s = "";
		for (int i = 0; i < k; i++) {
			s += " ";
		}
		return s;
	}

	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseClicked(MouseEvent arg0) {// 处理单击、双击
		// TODO Auto-generated method stub
		if (arg0.getClickCount() == 2) {// 双击
			menu1.setEnabled(false);
			int index = list.locationToIndex(arg0.getPoint());
			if (index >= rr.length) {
				return;
			}
			String s1 = rr[index].substring(rr[index].indexOf("}") + 1).trim();
			if (s1.indexOf("#") != -1) {
				s1 = s1.substring(0, s1.length() - 1);
				String dir = rr[index].substring(0, rr[index].indexOf("{"))
						.trim();
				DatagramSocket getSubdirs = null;
				if (rr[index].substring(rr[index].indexOf("{")).indexOf("X") == -1) {
					JOptionPane.showMessageDialog(this, "无目录执行权限!", "Error",
							JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						InetAddress add = InetAddress.getByName(ip.getText()
								.trim());
						getSubdirs = new DatagramSocket();
						String s = "SUB" + path.getText().trim() + "\\"
								+ dir.trim();
						byte[] sudirb = s.getBytes();
						DatagramPacket getSubdirsp = new DatagramPacket(sudirb,
								sudirb.length, add, 9871);
						getSubdirs.send(getSubdirsp);
						getSubdirs.close();
						DatagramSocket getSubdirserver = new DatagramSocket(
								9001);
						byte[] getSubdirs2b = new byte[40960];
						DatagramPacket getSubdirserverp = new DatagramPacket(
								getSubdirs2b, getSubdirs2b.length);
						getSubdirserver.receive(getSubdirserverp);
						getSubdirserver.close();
						String sss = new String(getSubdirserverp.getData(), 0,
								getSubdirserverp.getLength()).trim();
						if (sss.length() == 0) {
							JOptionPane.showMessageDialog(this, "空目录或不允许访问!",
									"Error", JOptionPane.ERROR_MESSAGE);
							menu1.setEnabled(true);
							return;
						}
						StringTokenizer stn = new StringTokenizer(sss, "?");
						path.setText(s.substring(3));
						rr = new String[stn.countTokens()];
						String[] ss = new String[rr.length];
						for (int i = 0; i < rr.length; i++) {
							rr[i] = stn.nextToken().trim();
							ss[i] = rr[i].substring(0, rr[i].indexOf("{"))
									.trim();
						}
						shua.setEnabled(false);
						list.removeAll();
						list = new JList(ss);
						list.addMouseListener(this);
						lmodel = list.getModel();
						pcenter.add(list);
						pcenter.getViewport().setView(list);
						this.validate();
						ToolTipManager.sharedInstance().registerComponent(list);

					} catch (Exception e) {
						if (getSubdirs != null && !getSubdirs.isClosed()) {

⌨️ 快捷键说明

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