我无法弄清楚如何使用async和future传输文件。 我有代码,当我运行此代码时,获取文件正在运行,但传输文件不起作用。
服务器
package test3;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class server3 {
private static final int PORT = 1234;
private static final String FILE_NAME = "2.jpg";
private static class Storage {
private final AsynchronousFileChannel fileChannel;
private Future<Integer> writeOperation, readOperation;
private long positionWrite, positionRead;
long counter =0;
Storage() throws IOException {
fileChannel = AsynchronousFileChannel.open(Paths.get(FILE_NAME),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.READ);
}
void fileReadAndWrite(ByteBuffer buffer, SocketChannel socketChannel)
throws InterruptedException, ExecutionException, IOException {
try {
if (readOperation != null)
positionRead += readOperation.get(5, TimeUnit.SECONDS);
readOperation = fileChannel.read(buffer, positionRead);
buffer.flip();
socketChannel.write(buffer);
} catch (TimeoutException exc) {
close();
System.out.println("Read timeout");
}
}
void write(ByteBuffer buffer) throws InterruptedException, ExecutionException {
try {
if (writeOperation != null)
positionWrite += writeOperation.get(5, TimeUnit.SECONDS);
writeOperation = fileChannel.write(buffer, positionWrite);
} catch (TimeoutException exc) {
close();
System.err.println("Write timeout");
}
}
void close() {
try {
fileChannel.close();
} catch (IOException exc) {
exc.printStackTrace();
}
}
}
public static void main(String args[]) throws Exception {
Selector selector = Selector.open();
try (ServerSocketChannel serverSocketChannel = ServerSocketChannel.open()) {
serverSocketChannel.bind(new InetSocketAddress(PORT));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
selector.select();
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> iterator = keys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
if (key.isAcceptable()) {
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ,
new Storage());
System.out.println("isAcceptable");
}
if (key.isReadable()) {
System.out.println("isReadable");
SocketChannel socketChannel = (SocketChannel) key.channel();
Storage storage = (Storage) key.attachment();
ByteBuffer buffer = ByteBuffer.allocate(256);
int read = socketChannel.read(buffer);
if (read != -1) {
buffer.flip();
storage.write(buffer);
} else {
storage.close();
socketChannel.close();
}
}
if (key.isWritable()) {
Storage storage = (Storage) key.attachment();
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
if (storage.fileChannel.size() >= storage.readOperation) {
storage.fileReadAndWrite(buffer, socketChannel);
} else {
storage.close();
socketChannel.close();
}
}
}
iterator.remove();
}
}
}
}
客户端
package test.test1;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.*;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class client_test1 {
private static final int PORT = 1234;
private static final String HOST = "localhost";
private static final String FILE_NAME = "3.jpg";
private static InetSocketAddress serverAddress = new InetSocketAddress(HOST, PORT);
public static void main(String[] args) {
try (SocketChannel socketChannel = SocketChannel.open(serverAddress)) {
try (FileChannel fileChannel = FileChannel.open(
Paths.get(FILE_NAME), StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
fileChannel.transferFrom(socketChannel, 0, Long.MAX_VALUE);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果频道已准备好接收新文件,请运行此行if (key.isWritable())
。这一行if (storage.counter != 30)
是错误的。此行调用,直到文件传输。我不明白如何传输文件。求助。