设计并实现一个基于字符界面的C/S结构的文件传输程序java代码
时间: 2023-12-19 16:03:55 浏览: 70
基于java的文件传输系统设计与实现毕业设计(项目报告+源代码).zip
以下是一个简单的基于字符界面的C/S结构的文件传输程序的Java代码,仅供参考:
服务端:
```java
import java.io.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
InputStream is = null;
OutputStream os = null;
try {
// 创建ServerSocket对象,监听指定端口
serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
// 等待客户端连接
socket = serverSocket.accept();
System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());
// 获取输入输出流
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
is = socket.getInputStream();
os = socket.getOutputStream();
// 接收文件名和文件大小
String fileName = in.readLine();
long fileSize = Long.parseLong(in.readLine());
System.out.println("接收到文件:" + fileName + ",大小:" + fileSize);
// 创建文件
File file = new File("D:\\" + fileName);
if (file.exists()) {
out.println("文件已存在");
continue;
} else {
out.println("OK");
}
// 接收文件内容并写入文件
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len;
long receivedSize = 0;
while (receivedSize < fileSize && (len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
receivedSize += len;
}
fos.close();
// 发送传输完成消息
out.println("文件传输完成");
// 关闭连接
socket.close();
System.out.println("客户端已断开连接:" + socket.getInetAddress().getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (is != null) is.close();
if (os != null) os.close();
if (in != null) in.close();
if (out != null) out.close();
if (socket != null) socket.close();
if (serverSocket != null) serverSocket.close();
}
}
}
```
客户端:
```java
import java.io.*;
import java.net.*;
public class FileClient {
public static void main(String[] args) throws IOException {
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
InputStream is = null;
OutputStream os = null;
try {
// 创建Socket对象,连接服务端
socket = new Socket("localhost", 8888);
System.out.println("连接服务器成功:" + socket.getInetAddress().getHostAddress());
// 获取输入输出流
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
is = socket.getInputStream();
os = socket.getOutputStream();
// 读取文件名并发送给服务端
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入文件名:");
String fileName = reader.readLine();
out.println(fileName);
// 判断文件是否存在
String response = in.readLine();
if ("文件已存在".equals(response)) {
System.out.println(response);
return;
}
// 读取文件大小并发送给服务端
System.out.print("请输入文件大小(字节):");
long fileSize = Long.parseLong(reader.readLine());
out.println(fileSize);
// 读取文件内容并发送给服务端
FileInputStream fis = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
int len;
long sentSize = 0;
while (sentSize < fileSize && (len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
sentSize += len;
}
fis.close();
// 接收传输完成消息
response = in.readLine();
System.out.println(response);
// 关闭连接
socket.close();
System.out.println("已断开与服务器的连接");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (is != null) is.close();
if (os != null) os.close();
if (in != null) in.close();
if (out != null) out.close();
if (socket != null) socket.close();
}
}
}
```
注意:以上代码仅为一个简单的示例,还有许多异常情况未处理,需要根据实际情况进行完善。
阅读全文