2016-09-26 7 views
0

ファイル名の送信を開始することによって、ファイルをIP(SocketChannel)上に移動しようとしています。私が信じている問題は、ByteBufferの読書にありますが、それを修正する方法がわかりません。ByteBuffer over SocketChannel

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); 

任意の助けいただければ幸いです。

多くのおかげ

+0

問題は何ですか?プログラムがどのように動作しているかは教えてくれませんでした。 –

+0

@JohnKugelmanこんにちはジョン私は混乱のために申し訳ありません、コードの最後のビットの上に問題を投稿しました... 個人的に私はファイル名が "null"文と、受信機にファイル名は常に空/ nullを – gcclinux

+0

詳細さ: Sendding nameBuffer:私はjava.nio.HeapByteBuffer [POS = 7 LIM = 7キャップ= 7]次に 受け取る: namebuffを受信:java.nio.HeapByteBuffer [pos = 7 lim = 512 cap = 512] 受信位置:7 ファイル名バイト:0 ファイル名:(空/ヌル) – gcclinux

答えて

0

は、テストのアロットと試みた後、私は最終的にのStringBuilderを使用して、私の問題を修正し、結果は私が望んでいたまさにです:ファイル名:

 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); 
関連する問題