Home Java javaTutorial java nio file transfer example

java nio file transfer example

Nov 11, 2016 am 10:42 AM

使用nio传输文件需要注意的是会出现粘包和服务器端缓冲区满的情况。第一种情况,客户端发送30次数据,而服务器端只接收到18次的情况,这种情况出现 主要是服务器端是以流的方式接收数据,它并不知道每次客户端传输数据的大小而造成的。第二种情况是服务器端缓冲区满,导致客户端数据传输失败,这种情况 下,需要判断传输int send = client.write(sendBuffer)的send值,如果send值为0,则服务器端的数据缓冲区可能满了。

客户端实现代码:

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Set;
  
public class FileClient {
    private int port = 8000;
    /* 发送数据缓冲区 */
    private static ByteBuffer sendBuffer = ByteBuffer.allocate(1024);
    /* 接受数据缓冲区 */
    private static ByteBuffer revBuffer = ByteBuffer.allocate(1024);
    private InetSocketAddress SERVER;
    private static Selector selector;
    private static SocketChannel client;
      
    public FileClient(){
        try{
            SERVER = new InetSocketAddress("localhost", port);
            init();
        }
        catch(Exception e){
            e.printStackTrace();
        }
          
    }
    private void init(){
        try {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.configureBlocking(false);
            selector = Selector.open();
            socketChannel.register(selector, SelectionKey.OP_CONNECT);
            socketChannel.connect(SERVER);
            while (true) {
                selector.select();
                Set<SelectionKey> keySet = selector.selectedKeys();
                for (final SelectionKey key : keySet) {
                    if(key.isConnectable()){
                        client = (SocketChannel)key.channel();
                        client.finishConnect();
                        client.register(selector, SelectionKey.OP_WRITE);
  
                    }
                    else if(key.isWritable()){
                        sendFile(client);
                    }
                }
                keySet.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
      
    private void sendFile(SocketChannel client) {
        FileInputStream fis = null;
        FileChannel channel = null;
        try {
//          fis = new FileInputStream("E:\\1.txt");
//          fis = new FileInputStream("E:\\1.rar");
            fis = new FileInputStream("G:\\3.rar");
            channel = fis.getChannel();
            int i = 1;
            int count = 0;
            while((count = channel.read(sendBuffer)) != -1) {
                sendBuffer.flip(); 
                int send = client.write(sendBuffer);
                System.out.println("i===========" + (i++) + "   count:" + count + " send:" + send);
                // 服务器端可能因为缓存区满,而导致数据传输失败,需要重新发送
                while(send == 0){
                    Thread.sleep(10);
                    send = client.write(sendBuffer);
                    System.out.println("i重新传输====" + i + "   count:" + count + " send:" + send);
                }
                sendBuffer.clear(); 
           }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
                fis.close();
                client.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
  
        }
    }
      
      
    public static void main(String[] args){
        new FileClient();
    }
}
Copy after login

服务器端

import java.io.FileOutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Set;
  
public class FileServer {
    private int port = 8000;
    /* 发送数据缓冲区 */
    private static ByteBuffer revBuffer = ByteBuffer.allocate(1024);
    private static Selector selector;
    private static FileOutputStream fout;
    private static FileChannel ch;
    public FileServer(){
        try{
            init();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    private void init() throws Exception{
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(new InetSocketAddress(port));
        selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("server start on port:" + port);
        while (true) {
            try {
                selector.select();// 返回值为本次触发的事件数
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                  
                for (SelectionKey key : selectionKeys) {
                    ServerSocketChannel server = null;
                    SocketChannel client = null;
                    int count = 0;
                    if (key.isAcceptable()) {
                        server = (ServerSocketChannel) key.channel();
                        System.out.println("有客户端连接进入=============)");
                        client = server.accept();
                        client.configureBlocking(false);
                        client.register(selector, SelectionKey.OP_READ);
                        fout = new FileOutputStream("G:\\" + client.hashCode() + ".rar");
                        ch = fout.getChannel();
                    } else if (key.isReadable()) {
                        client = (SocketChannel) key.channel();
                        revBuffer.clear();
                        count = client.read(revBuffer);
                        int k = 0;
                        // 循环读取缓存区的数据,
                        while(count > 0){
                            System.out.println("k=" + (k++) + " 读取到数据量:" + count);
                            revBuffer.flip();
                            ch.write(revBuffer);
                            fout.flush();
                            revBuffer.clear();
                            count = client.read(revBuffer);
                        }
                        if(count == -1){
                            client.close();
                            ch.close();
                            fout.close();
                        }
                    }
                    else if (key.isWritable()) {
                        System.out.println("selectionKey.isWritable()");
                      
                    }
                }
                System.out.println("=======selectionKeys.clear()");
                selectionKeys.clear();
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
  
        }
    }
    public static void main(String[] args){
        new FileServer();
    }
}
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

What is the reason why the browser does not respond after the WebSocket server returns 401? How to solve it? What is the reason why the browser does not respond after the WebSocket server returns 401? How to solve it? Apr 19, 2025 pm 02:21 PM

The browser's unresponsive method after the WebSocket server returns 401. When using Netty to develop a WebSocket server, you often encounter the need to verify the token. �...

How to analyze the cracking process of IntelliJ IDEA and find the lib or class responsible for registration? How to analyze the cracking process of IntelliJ IDEA and find the lib or class responsible for registration? Apr 19, 2025 pm 04:00 PM

Regarding the analysis method of IntelliJIDEA cracking in the programming world, IntelliJ...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

See all articles