我来为您讲解一下“Java使用NIO优化IO实现文件上传下载功能”的完整攻略。
概述
Java NIO library 是Java语言提供的一种基于缓冲区、非阻塞的IO,使得Java应用程序能够快速轻便地处理并发客户端请求。使用Java NIO实现文件上传下载功能的好处是可以大大提高系统的吞吐量、降低系统的IO延迟,而且还能避免阻塞线程,提高服务器的并发能力。
Java NIO
Java NIO的核心概念包括通道(Channel)、缓冲区(Buffer)、选择器(Selector)和文件锁(FileLock)。其中,通道负责连接数据源,缓冲区负责存储数据,选择器负责监听通道的事件,文件锁提供了文件级别的共享锁。
文件上传实现
- 创建文件上传的服务器端代码
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
public class NioServer {
public static void main(String[] args) throws Exception {
java.net.ServerSocket serverSocket = new java.net.ServerSocket(8888);
System.out.println("NioServer has started");
while (true) {
SocketChannel socketChannel = SocketChannel.open();
java.net.Socket socket = serverSocket.accept();
System.out.println(socket.getInetAddress() + " is connected");
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (socketChannel.read(buffer) != -1) {
buffer.flip();
Charset charset = Charset.forName("UTF-8");
String message = new String(buffer.array(), 0, buffer.limit(), charset);
System.out.println(message);
buffer.clear();
}
String fileName = "C:\\tmp\\fileupload\\temp.log";
FileChannel outChannel = new FileOutputStream(fileName).getChannel();
ByteBuffer buffer1 = ByteBuffer.allocate(1024);
long fileSize = socketChannel.read(buffer1);
while (fileSize > 0) {
buffer1.flip();
outChannel.write(buffer1);
buffer1.clear();
fileSize = socketChannel.read(buffer1);
}
File file = new File(fileName);
System.out.println(file.getName() + "has been saved at: " + file.getAbsolutePath());
outChannel.close();
socketChannel.close();
}
}
}
- 创建文件上传的客户端代码
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
public class NioClient {
public static void main(String[] args) throws Exception {
String filePath = "C:\\tmp\\fileupload\\test.log";
File file = new File(filePath);
String fileName = file.getName();
SocketChannel socketChannel = SocketChannel.open(new java.net.InetSocketAddress("localhost", 8888));
Charset charset = Charset.forName("UTF-8");
ByteBuffer buffer1 = ByteBuffer.allocate(1024);
buffer1.put(fileName.getBytes(charset));
buffer1.flip();
socketChannel.write(buffer1);
FileChannel inChannel = new FileInputStream(file).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (inChannel.read(buffer) != -1) {
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
}
inChannel.close();
socketChannel.close();
}
}
文件下载实现
- 创建文件下载的服务器端代码
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
public class NioServer {
public static void main(String[] args) throws Exception {
java.net.ServerSocket serverSocket = new java.net.ServerSocket(8888);
System.out.println("NioServer has started");
while (true) {
SocketChannel socketChannel = SocketChannel.open();
java.net.Socket socket = serverSocket.accept();
System.out.println(socket.getInetAddress() + " is connected");
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (socketChannel.read(buffer) != -1) {
buffer.flip();
Charset charset = Charset.forName("UTF-8");
String message = new String(buffer.array(), 0, buffer.limit(), charset);
System.out.println(message);
buffer.clear();
}
String filePath = "C:\\tmp\\filedownload\\" + message.trim();
File file = new File(filePath);
if (!file.exists()) {
System.out.println("File does not exist");
} else {
FileChannel inChannel = new FileInputStream(file).getChannel();
ByteBuffer buffer1 = ByteBuffer.allocate(1024);
int bytesRead;
while ((bytesRead = inChannel.read(buffer1)) != -1) {
buffer1.flip();
socketChannel.write(buffer1);
buffer1.clear();
}
System.out.println(file.getName() + "has been downloaded from: " + file.getAbsolutePath());
inChannel.close();
}
socketChannel.close();
}
}
}
- 创建文件下载的客户端代码
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
public class NioClient {
public static void main(String[] args) throws Exception {
String fileName = "test.log";
SocketChannel socketChannel = SocketChannel.open(new java.net.InetSocketAddress("localhost", 8888));
Charset charset = Charset.forName("UTF-8");
ByteBuffer buffer1 = ByteBuffer.allocate(1024);
buffer1.put(fileName.getBytes(charset));
buffer1.flip();
socketChannel.write(buffer1);
String filePath = "C:\\tmp\\filedownload\\temp.log";
FileChannel outChannel = new FileOutputStream(filePath).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
long fileSize = socketChannel.read(buffer);
while (fileSize > 0) {
buffer.flip();
outChannel.write(buffer);
buffer.clear();
fileSize = socketChannel.read(buffer);
}
File file = new File(filePath);
System.out.println(file.getName() + "has been saved at: " + file.getAbsolutePath());
outChannel.close();
socketChannel.close();
}
}
以上就是Java使用NIO优化IO实现文件上传下载功能的完整攻略,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java使用NIO优化IO实现文件上传下载功能 - Python技术站