0
我想通过开始发送文件名来获取通过IP(SocketChannel)传输的文件。我相信这个问题是在阅读ByteBuffer,但我不知道如何纠正它。通过SocketChannel的ByteBuffer
FileSender部分:
public void sendFile(SocketChannel socketChannel) {
try {
File file = new File("B:\\Software\\OLD.zip");
String filename = file.getName();
byte[] nameBytes = filename.getBytes();
ByteBuffer nameBuffer = ByteBuffer.wrap(nameBytes);
socketChannel.write(nameBuffer);
//FileChannel inChannel = aFile.getChannel();
FileChannel inChannel = FileChannel.open(file.toPath());
ByteBuffer buffer = ByteBuffer.allocate(32 * 1024); // 32.76800 kilobytes
System.out.println(" Sendding nameBuffer: "+nameBuffer);
int bytesread = inChannel.read(buffer);
while (bytesread != -1) {
buffer.flip();
socketChannel.write(buffer);
buffer.compact();
bytesread = inChannel.read(buffer);
}
Thread.sleep(1000);
System.out.println(" System Info! @LargeFileSender - End of file reached!");
socketChannel.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
这里是我认为是造成这一问题的FileReceiver的一部分。
public void readFileFromSocket(SocketChannel socketChannel) {
long startTime = System.currentTimeMillis();
try {
ByteBuffer namebuff = ByteBuffer.allocate(512);
socketChannel.read(namebuff);
System.out.println(" Receiving namebuff: " + namebuff);
byte[] namebyte = new byte[512];
String filename = "";
int position = namebuff.position();
System.out.println(" Receiving position: " + position);
while (namebuff.hasRemaining()) {
namebyte[position] = namebuff.get();
position = namebuff.position();
}
filename = new String(namebyte, 0, position);
System.out.println(" File namebyte: " + namebyte[7]);
System.out.println(" File Name: " + filename);
File file = new File(filename);
ByteBuffer buffer = ByteBuffer.allocate(32 * 1024); // 32.76800
FileOutputStream aFile = new FileOutputStream(file);
FileChannel fileChannel = aFile.getChannel();
BigDecimal bigDecimal = new BigDecimal(0.0);
BigDecimal kilobyteDecimal = new BigDecimal(32.76);
while (socketChannel.read(buffer) > 0) {
buffer.flip();
fileChannel.write(buffer);
buffer.compact();
bigDecimal = bigDecimal.add(kilobyteDecimal);
}
Thread.sleep(1000);
fileChannel.close();
buffer.clear();
ProgressDial.main("false");
Thread.sleep(2000);
System.out.println(" System Info! @FileReceiver - Transfer completed!");
socketChannel.close();
aFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
我个人认为,这个问题是这里的文件名是“空”
while (namebuff.hasRemaining()) {
namebyte[position] = namebuff.get();
position = namebuff.position();
}
filename = new String(namebyte, 0, position);
任何帮助将不胜感激。
千恩万谢
问题是什么?你没有告诉我们你的程序是如何运作的。 –
@JohnKugelman你好约翰我很抱歉的混淆,我没有发布问题的最后一位代码... 个人我相信问题是在这里,因为文件名是“null”,我添加了一些打印语句和接收器上的文件名是总是空/空 – gcclinux
详细说明: Sendding nameBuffer:java.nio.HeapByteBuffer [POS = 7 LIM = 7帽= 7] 然后我得到: 接收namebuff:java.nio.HeapByteBuffer [pos = 7 lim = 512 cap = 512] 接收位置:7 文件名字节:0 文件名:(空/空) – gcclinux