使用Java的Nio流进行文件传输案例。
FileReceiver
package com.nio.demo;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;
public class FileReceiver {
public static void main(String[] args) throws IOException {
FileReceiver fileSender = new FileReceiver();
SocketChannel clientSocket = fileSender.createSocketChannel();
fileSender.recFile(clientSocket);
}
/**
* 创建流渠道
*
* @return
* @throws IOException
*/
private SocketChannel createSocketChannel() throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(8081));
SocketChannel c