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

📄 ftpservice.java

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

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.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;

public class FtpService extends Thread {
	public Socket clients = null;

	BufferedReader in = null;

	PrintWriter out = null;

	public FtpService(Socket clients) {
		this.clients = clients;
	}

	public FtpService() {

	}

	public void init() {
		try {
			in = new BufferedReader(new InputStreamReader(clients
					.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			out = new PrintWriter(clients.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		init();
		boolean done = false;
		String order = "";
		while (!done) {
			if (!clients.isClosed()) {
				try {
					order = in.readLine();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (order != null) {
					if (order.startsWith("userlogin@")) {
						System.out.println(clients+"请求登录");
						userlogin(order);
					}
					if (order.equals("get")) {
						try {
							order = in.readLine();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println(clients+"请求下载文件"+order);
						dowloadFile(new File(order));
						// done = true;
					}
					if (order.equals("upfile")) {
						try {
							order = in.readLine();
						} catch (IOException e) {
							e.printStackTrace();
						}
						System.out.println(clients+"请求上传文件"+order);
						updateFile(new File(FileServeConstants.ftpfloder,
								order.toString()));
					}
					if (order.equals("exit")) {
						System.out.println(clients + "退出");
						try {
							clients.close();// 关闭连接
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					if (order.equals("dir")) {
						System.out.println(clients+"请求查看服务器端文件列表");
						getFileList();
					}
				}
			} else {
				done = true;
			}
		}
	}

	private void userlogin(String order) {
		init();
		boolean flag = false;
		String name = order.substring(15, order.lastIndexOf("@") - 9);
		String password = order.substring(order.lastIndexOf("@") + 1);
		userlogin us = new userlogin();
		if (us.checkUser(name, password)) {
			flag = true;
			System.out.println(clients+"成功登录");
		}
		out.println(flag);
		out.flush();
	}

	public void updateFile(File upfile) {
		String path=upfile.getAbsolutePath();
		File floder=new File(path.substring(0,path.lastIndexOf("\\")));
		if(!floder.exists()){
	      floder.mkdir();
		}
		init();
		String text = "";
		String line = "";
		try {
			line = in.readLine();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while (line != null) {
			text += line+"\n";
			try {
				line = in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			BufferedWriter buff = new BufferedWriter(new FileWriter(upfile));
			if (!text.equals("")) {
				buff.write(text);
				buff.flush();
			}
			buff.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void getFileList() {
		init();
		ObjectOutputStream p = null;
		try {
			p = new ObjectOutputStream(clients.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		FileObject fo = new FileObject();
		FileProcess f = new FileProcess();
		fo.setVector(f.getFileList());
		try {
			p.writeObject(fo);
			p.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void dowloadFile(File file) {
		RandomAccessFile outFile = null;
		OutputStream output = null;
		try {
			outFile = new RandomAccessFile(file, "r");
		} catch (FileNotFoundException e) {
			System.out.println("不存在文件!");
		}
		try {
			output = clients.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();
				System.out.println(clients+"文件下载完毕"+file.getAbsolutePath());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void startServe() {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(21);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("服务器端启动");
		System.out.println("Ftpserver starting up on port 21");
		System.out.println("Waiting for connection");
		for (;;) {
			try {
				Socket s = ss.accept();
				new FtpService(s).start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String args[]) {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(21);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("服务器端启动");
		System.out.println("Ftpserver starting up on port 21");
		System.out.println("Waiting for connection");
		for (;;) {
			try {
				Socket s = ss.accept();
				new FtpService(s).start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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