简体   繁体   English

SocketChannel上的ByteBuffer

[英]ByteBuffer over SocketChannel

I am trying to get File moved over IP (SocketChannel) by starting to send the file name. 我试图通过开始发送文件名来使文件通过IP(SocketChannel)移动。 The issue I believe is in the reading of the ByteBuffer but I am not sure how to correct it. 我认为问题出在ByteBuffer的读取上,但是我不确定如何更正它。

FileSender Part: 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();
    }

Here is the Part of the FileReceiver that I think is causing the issue. 这是我认为导致问题的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();
    }

}

personally I believe that the issue is here as the File Name is "null" 我个人认为问题出在这里,因为文件名是“ null”

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

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

Any help would be greatly appreciated. 任何帮助将不胜感激。

many Thanks 非常感谢

After allot of testing and trying I have finally fixed my issue using StringBuilder and the result is exactly what I wanted: FileName: 经过测试和尝试后,我终于使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SocketChannel.write(ByteBuffer [])“损坏”数据 - SocketChannel.write(ByteBuffer[]) “corrupting” data SocketChannel如何处理ByteBuffer中切掉的UTF-8字符和行 - SocketChannel how to deal with cutted UTF-8 chars and lines in ByteBuffer Windows中Android的Java SocketChannel写(ByteBuffer源)不同吗? - is Java SocketChannel write(ByteBuffer source) in Android different in windows? 通过socketChannel发送jpeg图像 - Sending a jpeg image over a socketChannel Java SocketChannel可以通过一个套接字通道发送多个文件吗? - Java SocketChannel send multiple files over one socketchannel possible? 通过SocketChannel传输文件时丢失字节 - Missing bytes while transferring file over SocketChannel 在Java中遍历char的Bytebuffer - Iterating over a Bytebuffer of chars in java Java NIO:每次我写入SocketChannel时创建ByteBuffer时分配或分配Direct - Java NIO: allocate or allocateDirect when creating a ByteBuffer every time I write to a SocketChannel 如何通过网络发送FlatBuffers ByteBuffer? - How to send FlatBuffers ByteBuffer over network? 为什么SocketChannel调用read(byteBuffer)引发java.net.SocketException:recvfrom失败:ECONNRESET(对等连接重置) - why does SocketChannel invoke read(byteBuffer) throw java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM