JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

JAVA NIO实现文件上传下载

wys521 2024-11-23 23:51:13 精选教程 20 ℃ 0 评论


首先,我们来看下NIO比java普通IO的优势:

  1. 非阻塞I/O:NIO的最大的特点就是支持非阻塞I/O操作。这意味着一个线程可以同时处理多个通道,提高了线程的利用率和系统的并发性能。
  2. 内存映射文件:NIO可以使用内存映射文件的方式访问文件,这种方式可以减少直接读写文件时的磁盘I/O操作,提高文件访问速度。
  3. 缓冲区:NIO使用缓冲区来存储数据,这样可以减少直接从流中读取数据的次数,提高数据读取效率。
  4. 通道:NIO使用通道来传输数据,通道可以复用,并且可以通过选择器来管理多个通道,实现异步I/O操作。
  5. 支持并行处理:由于NIO支持非阻塞I/O操作,因此可以同时处理多个任务,适合于需要并行处理的应用场景,比如网络通信、大规模数据处理等。

总的来说,Java NIO相比IO更适用于需要处理大规模数据、高并发请求或需要实现异步I/O操作的应用场景。

在Java中,使用NIO(Non-blocking I/O)进行文件上传和下载可以提供更高的性能和更好的用户体验。以下是一个简单的示例,说明如何使用Java NIO实现文件上传和下载。

文件上传

首先,我们需要一个服务器来接收上传的文件。

import java.io.IOException;  
import java.nio.ByteBuffer;  
import java.nio.channels.FileChannel;  
import java.nio.channels.SocketChannel;  
import java.nio.file.Paths;  
import java.nio.file.StandardOpenOption;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
  
public class FileUploadServer {  
    private static final int PORT = 8080;  
  
    public static void main(String[] args) throws IOException {  
        ExecutorService executorService = Executors.newFixedThreadPool(10);  
        try (ServerSocket serverSocket = new ServerSocket(PORT);  
             SocketChannel socketChannel = serverSocket.accept()) {  
            executorService.execute(() -> {  
                try (FileChannel fileChannel = FileChannel.open(Paths.get("uploaded-file.txt"), StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {  
                    ByteBuffer buffer = ByteBuffer.allocate(4096);  
                    while (socketChannel.read(buffer) != -1) {  
                        buffer.flip();  
                        fileChannel.write(buffer);  
                        buffer.clear();  
                    }  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            });  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            executorService.shutdown();  
        }  
    }  
}

文件下载

接下来,我们需要一个客户端来下载文件。

import java.io.IOException;  
import java.nio.ByteBuffer;  
import java.nio.channels.FileChannel;  
import java.nio.channels.SocketChannel;  
import java.nio.file.Paths;  
import java.nio.file.StandardOpenOption;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
  
public class FileDownloadClient {  
    private static final String HOST = "localhost";  
    private static final int PORT = 8080;  
    private static final String OUTPUT_FILE = "downloaded-file.txt";  
  
    public static void main(String[] args) throws IOException {  
        ExecutorService executorService = Executors.newFixedThreadPool(10);  
        try (SocketChannel socketChannel = SocketChannel.open(HOST, PORT)) {  
            executorService.execute(() -> {  
                try (FileChannel fileChannel = FileChannel.open(Paths.get(OUTPUT_FILE), StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {  
                    ByteBuffer buffer = ByteBuffer.allocate(4096);  
                    while (socketChannel.read(buffer) != -1) {  
                        buffer.flip();  
                        fileChannel.write(buffer);  
                        buffer.clear();  
                    }  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            });  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            executorService.shutdown();  
        }  
    }  
}

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表