Java:简单FTP服务器设计
大家好,我是京久。
进行Java学习需要实操。所以编写一个简单的FTP服务器。
目录
- 目的
- 使用设备及环境
- 功能
- 方案设计
- 代码实现
- 后记
目的
- 实现一个简单的FTP服务器客户端和服务器端程序,训练Socket,I/O编程技术,进一步巩固和提升应用开发技能。
- 熟悉Java代码编写规范。
使用设备及环境
设备:MacBook Pro(M1)
软件:eclipse
JDK:15.0.2
功能
- 文件上传
- 文件下载
方案设计
- 编写服务器端代码
- 编写客户端代码
- 编写工具类代码
代码实现
服务器端
//import已省略
public class FTPServer {private static String serverDir = "xxxx";//设置服务器端地址public static void main(String[] args) {System.out.println("========服务器已启动========");try {ServerSocket ss = new ServerSocket(5464);//端口号使用jing9键全拼Socket s = ss.accept();DataOutputStream dos = new DataOutputStream(s.getOutputStream());DataInputStream dis = new DataInputStream(s.getInputStream());//处理用户指令String command;while(true) {command = dis.readUTF();if(command.startsWith("put ")) {receive(command.substring(4),dis);}else if(command.startsWith("get ")) {send(command.substring(4),dos);}else if(command.startsWith("disconnect ")) {break;}}dis.close();dos.close();s.close();ss.close();System.out.println("======服务器已退出======");} catch (IOException e) {e.printStackTrace();}}public static void receive(String fname, DataInputStream dis){//文件接收方法try {Long l = dis.readLong();FileOutputStream fos = new FileOutputStream(serverDir+fname);for(int i=0;i<l;i++) {fos.write(dis.read());}fos.close();} catch (IOException e) {e.printStackTrace();System.out.println("接收失败");}System.out.println("接收成功");}public static void send(String fname, DataOutputStream dos) {//文件发送方法File f = new File(serverDir+fname);if(f.exists()) {try {dos.writeBoolean(true);long l = f.length();dos.writeLong(l);FileInputStream fis = new FileInputStream(f);for(int i=0;i<l;i++){dos.write(fis.read());}fis.close();} catch (IOException e) {e.printStackTrace();System.out.println("发送失败");}System.out.println("发送成功");}}
}
客户端
//import已省略
public class FTPClient{private static int port = 5464;private static String host = "localhost";private static boolean connected = false;private static Socket s = null;private static DataOutputStream dos = null;private static DataInputStream dis = null;private static String clientDir = "xxxx";//保存下载的文件地址public static void main(String[] args) {System.out.println("========客户端已经启动========");FTPClient.connect();System.out.println("输入get进行下载,put进行上传,exit进行退出");while (true) {System.out.println("ftp:");String command = MyTool.readline();if (command.startsWith("put ")) {FTPClient.put(command.substring(4));} else if (command.startsWith("get ")) {FTPClient.get(command.substring(4));} else if (command.equals("exit")) {break;} else {System.out.println("无效指令,请重新输入");}}FTPClient.disconnect();}private static void disconnect(){if(connected) {try {dos.writeUTF("disconnect ");dis.close();dos.close();s.close();} catch (IOException e) {e.printStackTrace();System.out.println("程序异常");}}connected = false;System.out.println("正常退出");}private static void get(String remoteFile) {try {dos.writeUTF("get "+remoteFile);boolean exist = dis.readBoolean();if(exist) {long size = dis.readLong();FileOutputStream fos = new FileOutputStream(clientDir + remoteFile);for(int i=0;i<size;i++) {fos.write(dis.read());}fos.close();}else {System.out.println("文件不存在");return;}} catch (IOException e) {e.printStackTrace();System.out.println("下载失败");}System.out.println("文件下载成功");}private static void put(String localFile) {try {File file = new File(localFile);if (file.exists()) {long size = file.length();dos.writeUTF("put " + file.getName());dos.writeLong(size);FileInputStream fis = new FileInputStream(localFile);for(int i=0;i<size;i++){dos.write(fis.read());}fis.close();}else {System.out.println("文件不存在,上传失败");return;}} catch (IOException e) {e.printStackTrace();System.out.println("上传失败");}System.out.println("上传成功");}public static void connect() {//连接方法try {s = new Socket(host,port);dis = new DataInputStream(s.getInputStream());dos = new DataOutputStream(s.getOutputStream());} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}connected = true;System.out.println("连接成功");}
}
工具类
//import已省略
public class MyTool {public static String readline() {String string = null;InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);try {string = br.readLine();} catch (IOException e) {e.printStackTrace();}return string;}}
后记
目前只做了单线程。部分交互可能做的不完善,欢迎评论指正!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
