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

📄 ftpuserscreen.java

📁 基于swing的java模拟的c/s服务模式
💻 JAVA
字号:
package clients;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;

import service.FileObject;

public class FtpUserScreen extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private JFrame f = null;

	private JMenuItem DowloadFileItem;

	private JMenuItem upFileItem;

	private JButton UpFileButton;

	private JButton DownloadButton;

	public JButton closeButton;

	public JButton reButton;

	private JButton listButton;

	private JTable table;

	private DefaultTableModel model;

	private Socket s;

	PrintWriter out = null;

	BufferedReader bu = null;

	public String user;

	String host;// 取得本机地址
   int localport;
	int port;// 取得本机端口

	public FtpUserScreen(String user, Socket s) {
		f = new JFrame("客户端管理程序" + user);
		this.s = s;
		this.user = user;
		host = s.getInetAddress().getHostName();// 取得本机地址
		port = s.getPort();// 取得本机端口
		localport=s.getLocalPort();
		init();
	}

	public void actionPerformed(ActionEvent arg0) {
		Object o = arg0.getSource();
		if (o.equals(JFrame.EXIT_ON_CLOSE)) {
			initpara();
			out.println("exit");
			out.flush();
			try {
				s.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void connect() {
		if (s.isClosed()) {
			System.out.println("socket closed,open it");
			try {
				s = new Socket(host, port); 
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void downloadFile(String file) {
		initpara();
		String filename = file.substring(file.lastIndexOf("\\") + 1);// 取得文件名
		out.println("get");
		out.println(file);
		out.flush();
		String text = "";
		String line = "";
		try {
			line = bu.readLine();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while (line != null) {
			text += line;
			try {
				line = bu.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (!FileClientConstant.savefloder.exists()) {
			FileClientConstant.savefloder.mkdir();
		}
		BufferedWriter buff = null;
		try {
			buff = new BufferedWriter(new FileWriter(new File(
					FileClientConstant.savefloder, filename)));
			if (!text.equals("")) {
				buff.write(text);
				buff.flush();
			}
			JOptionPane.showMessageDialog(this, "下载完毕,文件保存在:"
					+ FileClientConstant.savefloder + "\\" + filename);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, "下载文件"
					+ FileClientConstant.savefloder + "\\" + filename
					+ "出错,请重新下载:");
		}
		try {
			out.close();
			bu.close();
			buff.close();
			s.close();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, "文件关闭异常");
		}

	}

	private void exit() {
		initpara();
		out.println("exit");
		out.flush();
		try {
			if (s != null) {
				s.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JOptionPane.showMessageDialog(this, "以和服务器端开连接");
	}

	// 菜单编辑目录
	public JMenu getEditMenu() {
		JMenu editMenu = new JMenu("编辑(E)");
		editMenu.setMnemonic('E');
		DowloadFileItem = new JMenuItem("下载文件(D)", 'D');
		upFileItem = new JMenuItem("上传文件(U)", 'U');
		DowloadFileItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {

			}
		});

		upFileItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {

			}
		});

		editMenu.add(DowloadFileItem);
		editMenu.add(upFileItem);
		return editMenu;
	}

	public void getFileList() {
		initpara();
		out.println("dir");
		out.flush();
		ObjectInputStream q = null;
		try {
			q = new ObjectInputStream(s.getInputStream());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		FileObject fo = null;
		try {
			try {
				fo = (FileObject) q.readObject();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		showResult(fo.getVector());
	}

	// 帮助目录
	public JMenu getHelpMenu() {
		JMenu help = new JMenu("帮助(H)");
		help.setMnemonic('H');
		JMenuItem h = new JMenuItem("帮助(H)", 'H');
		help.add(h);
		return help;
	}

	public JMenuBar getmenuBar() {
		JMenuBar menubar = new JMenuBar();
		menubar.add(getSystemMenu());
		menubar.add(getEditMenu());
		menubar.add(getHelpMenu());
		return menubar;
	}

	public JScrollPane getPane() {
		model = new DefaultTableModel();
		table = new JTable(model);
		JScrollPane scrollpane = new JScrollPane();
		model.addColumn("文件名");
		model.addColumn("大小");
		model.addColumn("修改日期");
		scrollpane.getViewport().add(table, null);
		return scrollpane;
	}

	// 系统目录
	public JMenu getSystemMenu() {
		JMenu systemmenu;// 系统目录
		JMenuItem existitem;
		systemmenu = new JMenu("文件(F)");
		systemmenu.setMnemonic('F');
		existitem = new JMenuItem("退出(E)", 'E');
		systemmenu.add(existitem);
		existitem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				f.dispose();
				System.exit(0);
			}
		});
		return systemmenu;
	}

	// 工具栏
	public JToolBar getToolBar() {
		JToolBar toolBar = new JToolBar();
		listButton = new JButton("文件列表", new ImageIcon(getClass().getResource(
				"image/add.gif")));
		UpFileButton = new JButton("上传文件", new ImageIcon(getClass()
				.getResource("image/add.gif")));

		DownloadButton = new JButton("下载文件", new ImageIcon(getClass()
				.getResource("image/change.gif")));
		closeButton = new JButton("断开连接", new ImageIcon(getClass().getResource(
				"image/change.gif")));
		reButton = new JButton("重新连接", new ImageIcon(getClass().getResource(
				"image/change.gif")));
		listButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				connect();
				DownloadButton.setEnabled(true);
				getFileList();

			}

		});
		reButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				out.println("exit");
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				f.dispose();
				new LoginScreen().setVisible(true);
			}
		});
		UpFileButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				connect();
				upfile();
			}
		});
		closeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				exit();
				closeButton.setEnabled(false);
				UpFileButton.setEnabled(false);
				DownloadButton.setEnabled(false);
				listButton.setEnabled(false);
			}

		});
		DownloadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				connect();
				String file = table.getValueAt(table.getSelectedRow(), 0)
						.toString();
				downloadFile(file);

			}
		});
		toolBar.add(listButton);
		toolBar.add(UpFileButton);
		toolBar.add(closeButton);
		toolBar.add(DownloadButton);
		toolBar.add(reButton);
		return toolBar;
	}

	// 文件选择
	public File getUpFile() {
		File select = null;
		JFileChooser file = new JFileChooser();
		int option = file.showOpenDialog(f);
		if (option == JFileChooser.APPROVE_OPTION) {
			select = file.getSelectedFile();
		}
		return select;
	}

	// 初始化
	public void init() {
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().setLayout(new BorderLayout());
		f.setLocation(150, 50);
		f.setSize(800, 650);
		f.setJMenuBar(getmenuBar());
		f.getContentPane().add(getToolBar(), BorderLayout.NORTH);
		f.getContentPane().add(getPane(), BorderLayout.CENTER);
		f.setVisible(true);
		DownloadButton.setEnabled(false);
		DowloadFileItem.setEnabled(false);

	}

	public void initpara() {
		try {
			out = new PrintWriter(s.getOutputStream());
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		try {
			bu = new BufferedReader(new InputStreamReader(s.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void showResult(Vector v) {
		if (model.getRowCount() > 0) {
			for (int i = 0; i < model.getRowCount(); i++) {
				model.removeRow(i);
			}
		}
		for (int i = 0; i < v.size();) {
			Object data[] = { v.elementAt(i), v.elementAt(i + 1),
					v.elementAt(i + 2) };
			model.addRow(data);
			i += 3;
		}
	}

	public void upfile() {
		initpara();
		File f = getUpFile();
		out.println("upfile");
		out.println(user + "\\" + f.getName());
		out.flush();
		RandomAccessFile outFile = null;
		OutputStream output = null;
		try {
			outFile = new RandomAccessFile(f, "r");
		} catch (FileNotFoundException e) {
			System.out.println("不存在文件!");
		}
		try {
			output = s.getOutputStream();
		} catch (IOException e) {
			System.out.println("与客户端连接失败!");
		}
		byte[] byteBuff = new byte[1024];
		int amount;
		if (output != null) {
			try {
				while ((amount = outFile.read(byteBuff)) != -1) {
					output.write(byteBuff, 0, amount);
				}
				output.close();
				out.close();
				s.close();
				outFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		JOptionPane.showMessageDialog(this, "文件已上传到你在服务器里的个人文件夹中");
	}
}

⌨️ 快捷键说明

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