编写Java程序,用Socket通信机制在服务器端和客户端之间传输文件。把代码敲出来
时间: 2024-05-10 11:21:10 浏览: 138
对于编写Java程序,用Socket通信机制在服务器端和客户端之间传输文件,以下是通用的示例代码:
Server代码:
```java
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
// 开始监听端口
System.out.println("Server listening on port 9999");
while(true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());
new Thread(new ClientHandler(clientSocket)).start();
}
}
// 处理Client连接的单独线程
private static class ClientHandler implements Runnable {
private Socket clientSocket;
public ClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
DataInputStream dis = null;
try {
dis = new DataInputStream(clientSocket.getInputStream());
String fileName = dis.readUTF();
long fileSize = dis.readLong();
Path filePath = Paths.get(fileName);
System.out.println("Receiving file: " + fileName + ", size: " + fileSize + " bytes");
byte[] buffer = new byte[1024];
int readBytes = 0;
// 创建输出的文件
File outputFile = new File("output_" + fileName);
while (fileSize > 0 && (readBytes = dis.read(buffer, 0, (int)Math.min(buffer.length, fileSize))) != -1) {
Files.write(outputFile.toPath(), buffer, 0, readBytes);
fileSize -= readBytes;
}
System.out.println("File saved to: " + outputFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null) dis.close();
if (clientSocket != null) clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
Client代码:
```java
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
final String fileName = "example.txt";
final String serverHost = "localhost";
final int serverPort = 9999;
Socket clientSocket = null;
BufferedInputStream bis = null;
DataOutputStream dos = null;
try {
clientSocket = new Socket(serverHost, serverPort);
bis = new BufferedInputStream(new FileInputStream(fileName));
dos = new DataOutputStream(clientSocket.getOutputStream());
dos.writeUTF(fileName);
dos.writeLong(new File(fileName).length());
byte[] buffer = new byte[1024];
int readBytes;
while ((readBytes = bis.read(buffer)) != -1) {
dos.write(buffer, 0, readBytes);
}
} finally {
// 关闭连接和文件流
try {
if (bis != null) bis.close();
if (dos != null) dos.close();
if (clientSocket != null) clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
使用以上代码可以在服务端和客户端间通过Socket传输文件。编译运行前先在服务器运行Server.java,然后在客户端运行Client.java即可。
阅读全文