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

📄 sqk.java

📁 局域网传输共享文件
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public class SQK extends JFrame implements ActionListener, Runnable {
	// 121.250.218.56
	boolean cansend = false;

	String tongyi2 = "3";

	int tongyi = 3;

	JFrame f;

	static String filename, path;

	JTextArea msg, show;

	DatagramSocket in, out;

	JTextField send, inetip;

	DatagramPacket inp, outp;

	byte[] buff;

	JLabel l1 = new JLabel("发送端口号:");

	JLabel l3 = new JLabel("对方IP:");

	JButton l4 = new JButton("开始");

	JButton l5, l6;

	Thread z;

	static int port;
String sss;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SQK test = new SQK("121.250.218.232");
		test.setVisible(true);
	}

	public SQK(String ss) {
		z = new Thread(this);
		z.setPriority(Thread.MIN_PRIORITY);
		z.start();
		sss=ss;
		setTitle("SQK聊天");
		setSize(500, 500);
		Dimension sc = getToolkit().getScreenSize();
		Dimension sz = getSize();
		setLocation(sc.width / 2 - sz.width / 2, sc.height / 2 - sz.height / 2);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				dispose();
			}
		});
		Container con = getContentPane();
		l4.setIcon(new ImageIcon("Fwdw_icons_06.png"));
		l4.addActionListener(this);
		send = new JTextField(16);
		inetip = new JTextField(16);
		inetip.setText(sss);
		inetip.setEditable(false);
		JPanel p1 = new JPanel();
		p1.setLayout(new FlowLayout());
		p1.add(l3);
		p1.add(inetip);
		con.add(p1, "North");
		// 显示聊天语句区
		msg = new JTextArea(20, 10);
		msg.setForeground(Color.green);
		msg.setLineWrap(true);
		JScrollPane themsg = new JScrollPane(msg);
		themsg
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

		msg.setEditable(false);
		con.add(themsg);

		JPanel p2 = new JPanel();
		p2.setLayout(new FlowLayout());
		show = new JTextArea(4, 35);
		show.setLineWrap(true);
		JScrollPane theshow = new JScrollPane(show);
		themsg
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		l5 = new JButton("发送");
		l5.addActionListener(this);
		p2.add(theshow);
		p2.add(l5);
		l6 = new JButton("发送文件");
		l6.addActionListener(this);
		p2.add(l6);
		con.add(p2, "South");
		new fasong().start();
		new jieshou().start();
	}

	public void actionPerformed(ActionEvent e) {
		String s1 = e.getActionCommand();
		InetAddress add;

		try {
			// 启动文件发送

			// 按钮
			if (s1.equals("开始")) {
				l5.setEnabled(true);
				l6.setEnabled(true);
				l4.setEnabled(false);
				l1.setEnabled(false);
				l3.setEnabled(false);
				
			} else if (s1.equals("发送")) {
				String s2 = show.getText();
				show.setText("");
				msg.append("我说:    " + s2 + "\n");
				// port = Integer.parseInt(send.getText().trim());
				add = InetAddress.getByName(sss);
				out = new DatagramSocket();

				buff = s2.getBytes();
				outp = new DatagramPacket(buff, buff.length, add, 4444);
				out.send(outp);

			} else if (s1.equals("发送文件")) {
				FileDialog fd = new FileDialog(new JFrame(), "SQK聊天",
						FileDialog.LOAD);
				fd.show(true);
				filename = fd.getFile();
				path = fd.getDirectory();
				String s = filename + path;
				if (filename != null) {
					msg.append(s + "   可以发送,正在发送\n");
					cansend = true;
				}
				// buff=fi.getBytes();
			}

		} catch (SocketException x) {
			msg.append("SocketException\n");
		} catch (UnknownHostException x) {
			msg.append("无法连接\n");
		} catch (IOException x) {
			msg.append("无法传送\n");
		}
	}

	public void run() {
		while (true) {
			try {
				in = new DatagramSocket(4444);
				byte[] buf = new byte[1024];
				inp = new DatagramPacket(buf, buf.length);
				in.receive(inp);
				String s = "对方说:"
						+ new String(inp.getData(), 0, inp.getLength());
				msg.append(s + "\n");
				in.close();
			} catch (Exception e) {
				msg.append("接收失败\n");
			}
		}
	}

	class fasong extends Thread {
		public void run() {
			while (true) {
				while (!cansend) {
				}
				try {
					Socket sends = new Socket(InetAddress.getByName(inetip
							.getText()), 5555);
					System.out.println("1");
					OutputStream netOut = sends.getOutputStream();
					OutputStream doc = new DataOutputStream(
							new BufferedOutputStream(netOut));
					byte[] b = new byte[2048];
					System.out.println("2");
					b = filename.getBytes();
					doc.write(b, 0, b.length);
					System.out.println("3");
					doc.flush();
					doc.close();
					netOut.close();

					// //////////
					ServerSocket ss = new ServerSocket(6666);
					while (tongyi2.equals("3")) {
						Socket acc = ss.accept();
						InputStream netIn = acc.getInputStream();
						BufferedInputStream in1 = new BufferedInputStream(netIn);
						byte[] b2 = new byte[1];
						int nu = in1.read(b2);
						while (nu > 0) {
							nu = in1.read(b2);
						}
						System.out.println("grthgtrb");
						tongyi2 = new String(b2).trim();
						System.out.println("grthgtrb");
						acc.close();
						in1.close();
						netIn.close();
						ss.close();
					}
					while (tongyi2.equals("1")) {
						System.out.println("frtg");
						Socket sends2 = new Socket(InetAddress
								.getByName("121.250.218.232"), 7777);
						// InetAddress.getByName(inetip.getText())
						System.out.println("bnxfghfghdfgjdfjftjhfyjdtyj");
						OutputStream netOut2 = sends2.getOutputStream();
						OutputStream doc2 = new BufferedOutputStream(netOut2);
						File wj = new File(SQK.path, SQK.filename);
						FileInputStream wjstream = new FileInputStream(wj);
						byte[] b3 = new byte[40960];
						int nu = wjstream.read(b3);
						while (nu != -1) {
							doc2.write(b3, 0, nu);
							doc2.flush();
							nu = wjstream.read(b3);

						}
						//doc2.flush();
						msg.append("发送完成.\n");
						wjstream.close();
						doc.close();
						sends2.close();
						netOut2.close();
						cansend = false;
						tongyi2 = "3";

					}
					while (tongyi2 .equals("2") ) {
						msg.append("对方拒绝接收.\n");
						sends.close();
						cansend = false;
						tongyi2 = "3";
					}

				} catch (SocketException e) {
					e.printStackTrace();
					msg.append("发送出错\n");
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}

			}
		}

	}

	class jieshou extends Thread {
		public void run() {
			try {
				InetAddress add = null;
				ServerSocket jieshous = new ServerSocket(5555);
				while (true) {
					Socket recieves = jieshous.accept();
					add = recieves.getInetAddress();
					InputStream recieveStream = recieves.getInputStream();
					BufferedInputStream xinhao = new BufferedInputStream(
							recieveStream);
					byte[] b1 = new byte[2048];
					int num = xinhao.read(b1);
					while (num > 0) {
						num = xinhao.read(b1);
					}
					String xinhaos = new String(b1).trim();
					new duihua(xinhaos, recieves);

					recieves.close();
					recieveStream.close();
					xinhao.close();
					while (tongyi == 3) {
					}
					if (tongyi == 1) {
						msg.append("正在接收......>>");
						Socket xinhaofasong = new Socket(add, 6666);
						OutputStream output = xinhaofasong.getOutputStream();
						BufferedOutputStream fasong = new BufferedOutputStream(output);
						byte[] b = new byte[1];
						b = Integer.toString(tongyi).getBytes();
						if (b.length > 0) {

							fasong.write(b, 0, b.length);
							fasong.flush();
						}
						fasong.close();
						output.close();
						File file = new File(xinhaos);
						file.createNewFile();
						FileOutputStream out = new FileOutputStream(file);
						ServerSocket jieshouwj = new ServerSocket(7777);
						Socket jieshou = jieshouwj.accept();

						InputStream jieshouStream = jieshou.getInputStream();
						BufferedInputStream js = new BufferedInputStream(
								jieshouStream);
						byte[] jbyte = new byte[40960];
						int nu = js.read(jbyte);
						while (nu != -1) {
							out.write(jbyte, 0, nu);
							nu = js.read(jbyte);
						}
						System.out.println("bfgbnrtmdnhnrnnnnnnnnnnnnn");
						out.flush();
						out.close();
						jieshouwj.close();
						jieshouStream.close();
						js.close();
						jieshou.close();

						tongyi = 3;
						cansend = false;
					} else if (tongyi == 2) {
						Socket xinhaofasong = new Socket(recieves
								.getInetAddress(), 6666);
						OutputStream output = xinhaofasong.getOutputStream();
						OutputStream fasong = new DataOutputStream(
								new BufferedOutputStream(output));
						byte[] b = new byte[1];
						b = Integer.toString(tongyi).getBytes();
						fasong.write(b, 0, b.length);
						fasong.close();
						output.close();
						tongyi = 3;
						cansend = false;
					}

					// 发送同意信号

				}
			} catch (Exception e) {
				msg.append("接收出错.\n");
			}
		}
	}

	private class duihua extends JFrame implements ActionListener {
		public duihua(String xinhaos, Socket recieves) {
			f = new JFrame();
			f.setSize(200, 200);
			f.setTitle("接收到文件信号");
			Container con = f.getContentPane();
			con.add(new JLabel("文件\"" + xinhaos + "\"来自"
					+ recieves.getInetAddress().getHostName() + "是否接收?"));
			JPanel p = new JPanel();
			p.setLayout(new FlowLayout());
			JButton b;
			b = new JButton("同意");
			b.addActionListener(this);
			p.add(b);
			b = new JButton("不同意");
			b.addActionListener(this);
			p.add(b);
			con.add(p, "South");
			f.setVisible(true);
		}

		public void actionPerformed(ActionEvent e) {
			String s = e.getActionCommand();
			if (s.equals("同意")) {
				tongyi = 1;
				f.dispose();
			} else if (s.equals("不同意")) {
				tongyi = 2;
				f.dispose();
			}
		}
	}
}

⌨️ 快捷键说明

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