在 Java 中使用 Socket 进行文件传输有多种方式,我将为你介绍两种常见的方式:基于普通的 Socket 编程和基于 NIO(New I/O)的方式。每种方式都会包括详细的步骤流程和示例代码。
这种方式使用普通的阻塞式 Socket 编程来实现文件传输。以下是步骤流程:
步骤流程:
示例代码:
服务器端:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Server waiting for connection...");
Socket socket = serverSocket.accept();
System.out.println("Connection established.");
InputStream inputStream = socket.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("received_file.txt");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
System.out.println("File received successfully.");
fileOutputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
客户端端:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 12345);
File file = new File("file_to_send.txt");
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream outputStream = socket.getOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("File sent successfully.");
outputStream.close();
fileInputStream.close();
socket.close();
}
}
Maven 依赖: 无需额外的 Maven 依赖。
这种方式使用 Java NIO 中的通道(Channel)和缓冲区(Buffer)来实现非阻塞的文件传输。以下是步骤流程:
步骤流程:
示例代码:
服务器端:
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
public class ServerNIO {
public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(12345));
serverSocketChannel.configureBlocking(false);
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("Server waiting for connection...");
while (true) {
selector.select();
for (SelectionKey key : selector.selectedKeys()) {
if (key.isAcceptable()) {
SocketChannel clientChannel = serverSocketChannel.accept();
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_READ);
System.out.println("Connection established.");
} else if (key.isReadable()) {
SocketChannel clientChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
FileChannel fileChannel = new FileOutputStream("received_file_nio.txt").getChannel();
int bytesRead;
while ((bytesRead = clientChannel.read(buffer)) > 0 || buffer.position() > 0) {
buffer.flip();
fileChannel.write(buffer);
buffer.compact();
}
fileChannel.close();
clientChannel.close();
System.out.println("File received successfully.");
}
}
selector.selectedKeys().clear();
}
}
}
客户端端:
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
public class ClientNIO {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 12345));
File file = new File("file_to_send.txt");
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (fileChannel.read(buffer) > 0 || buffer.position() > 0) {
buffer.flip();
socketChannel.write(buffer);
buffer.compact();
}
System.out.println("File sent successfully.");
fileChannel.close();
socketChannel.close();
}
}
Maven 依赖: 无需额外的 Maven 依赖。
以上是基于普通 Socket 编程和基于 NIO 的两种文件传输方式的示例代码和步骤流程。你可以根据自己的需求选择适合的方式来实现文件传输。