碰巧服务器接收到文件并通过一个端口将其发送给我......
在第一种情况下,根据服务器的响应,传输成功:
SocketAddress SocketAdres = new InetSocketAddress(Server, FilePort);
socketChannel = SocketChannel.open();
socketChannel.connect(SocketAdres);
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
fileChannel.transferTo(0, fileChannel.size(), socketChannel);
socketChannel.close();
fileChannel.close();
再进一步,我们开始遇到问题:
int Size = Integer.parseInt(Answer);
File file2 = new File("/storage/emulated/0/download/" + "Dowland.db");
file2.createNewFile();
SocketAddress DataImport = new InetSocketAddress(Server, FilePort);
socketChannel = SocketChannel.open(DataImport);
socketChannel.connect(DataImport);
RandomAccessFile ra= new RandomAccessFile(file2,"rw");
FileChannel fileChannel = ra.getChannel();
fileChannel.transferFrom(socketChannel, 0, Size);
socketChannel.close();
fileChannel.close();
如果我这样做,我会得到:
Caused by: java.nio.channels.AlreadyConnectedException
,尽管通道关闭了...
在这一行中:socketChannel.connect(DataImport);
如果我们尝试通过一个相同的 socketChannel 来做所有事情,那么什么都不会发生......
考虑到以前问题的经验——我们有权访问互联网和写入、读取外部存储。
请帮忙)
另一个不为人知的错误:
我们已经连接了这个命令,我们再次尝试:
它需要被移除