NIO上传
NIO Client
package com.example.leetcode.file;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
public class NewIoClient {
public static void main(String[] args) throws Exception {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("localhost", 8899));
socketChannel.configureBlocking(true);
String fileName = "D:/Download/spark-2.4.0-bin-hadoop2.7.tgz";
FileChannel channel = new FileInputStream(fileName).getChannel();
long size = channel.size();
long position = 0;
long totalCount = 0;
long startTime = System.currentTimeMillis();
while (size > 0) {
long count = channel.transferTo(position, channel.size(), socketChannel);
position += count;
size -= count;
totalCount += count;
}
System.out.println("发送字节数:" + totalCount + ", 耗时:" + (System.currentTimeMillis() - startTime));
channel.close();
socketChannel.close();
}
}
NIO Server
package com.example.leetcode.file;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class NewIoServer {
public static void main(String[] args) throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
ServerSocket socket = serverSocketChannel.socket();
// 当Socket连接关闭时,并不会立刻释放端口,而是处于超时状态并继续持有端口一段时间
// 如果端口号处于TIME_WAIT状态时,通过设置该参数为True,新的socket是可以绑定该端口号的
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(8899));
ByteBuffer buffer = ByteBuffer.allocate(4096);
while (true) {
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(true);
int read = 0;
long count = 0;
while (read != -1) {
read = socketChannel.read(buffer);
buffer.rewind();
count += read;
}
System.out.println("发送字节数:" + count);
}
}
}
上传文件(OLD)
Client
package com.example.leetcode.file;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.net.Socket;
public class OldIoClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 8899);
String fileName = "D:/Download/spark-2.4.0-bin-hadoop2.7.tgz";
FileInputStream fileInputStream = new FileInputStream(fileName);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
byte[] buffer = new byte[4096];
long startTime = System.currentTimeMillis();
int read = 0;
long count = 0;
while (read >= 0) {
read = dataInputStream.read(buffer);
count += read;
dataOutputStream.write(buffer);
}
System.out.println("发送字节数:" + count + ", 耗时:" + (System.currentTimeMillis() - startTime));
dataOutputStream.close();
dataInputStream.close();
socket.close();
}
}
Server
package com.example.leetcode.file;
import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class OldIoServer {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8899);
while (true) {
Socket socket = serverSocket.accept();
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
byte[] buffer = new byte[4096];
try {
while (true) {
int read = dataInputStream.read(buffer);
if (read == -1) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}