ByteBuffer over SocketChannel

时间:2016-09-26 14:58:16

标签: java bytebuffer socketchannel

我试图通过开始发送文件名来使文件移动到IP(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();
    }

}

我个人认为问题出在这里,因为文件名是" null"

            while (namebuff.hasRemaining()) {
            namebyte[position] = namebuff.get();
            position = namebuff.position();
        }

        filename = new String(namebyte, 0, position);

非常感谢任何帮助。

非常感谢

1 个答案:

答案 0 :(得分:0)

经过大量的测试和尝试后,我终于使用StringBuilder修复了我的问题,结果正是我想要的:FileName:

        try {

        ByteBuffer namebuff = ByteBuffer.allocate(256);
        socketChannel.read(namebuff);
        int position = namebuff.position();
        namebuff.rewind();
        String filename = "";
        int startPosition = 0;

        System.out.println(" Receiving position: "+position);
        StringBuilder sb = new StringBuilder();

        while (startPosition < position) {
            sb.append((char)namebuff.get());
            startPosition++;
        }

        filename = sb.toString();
        System.out.println(" FileName: "+filename);