android java Socket多文件发送

来源:互联网 发布:网络交换机机柜 编辑:程序博客网 时间:2024/05/08 14:03
android java Socket多文件发送       
 

     

[html] view plaincopyprint?
  1. for (int i =0;i <files.length; i++) { 
  2.                 byte[] namebyte = files[i].getName().getBytes("UTF-8"); 
  3.                 long size =files[i].length(); 
  4.                 int nameLength = namebyte.length; 
  5.                 fileChannel =new FileInputStream(files[i]).getChannel(); 
  6.                 ByteBuffer buffer =ByteBuffer.allocate(1024); 
  7.                 buffer.clear(); 
  8.                 buffer.putInt(4+8+nameLength); 
  9.                 buffer.putInt(nameLength); 
  10.                 buffer.put(namebyte); 
  11.                 buffer.putLong(size); 
  12.                 buffer.flip(); 
  13.                 while(buffer.hasRemaining()){ 
  14.                     sc.write(buffer); 
  15.                 } 
  16.                 long count = 1024*1024; 
  17.                 long read =0L
  18.                 while(read < size){ 
  19.                     if(size - read <count
  20.                         count = size - read; 
  21.                     read += fileChannel.transferTo(0+read, count, sc); 
  22.                     System.out.println("read:"+read); 
  23.                 } 
  24.                 fileChannel.close(); 
  25.                 if(i < files.length -1){ 
  26.                     sc.write(ByteBuffer.wrap(new byte[]{1})); 
  27.                     System.out.println(1); 
  28.                 } 
  29.                 else 
  30.                     sc.write(ByteBuffer.wrap(new byte[]{0})); 
  31.             } 


    接收方核心代码,由于发送的时候文件的信息,如:文件名和文件大小是分开发送的,所以接收的时候也是分开接收的,多个文件的时候接收方式一个一个接收的。

   

[html] view plaincopyprint?
  1. private void parseHead(int headlength) { 
  2.         buffer = ByteBuffer.allocate(headlength); 
  3.         try { 
  4.             while(buffer.position() < buffer.capacity()) 
  5.               clientChannel.read(buffer); 
  6.             buffer.flip(); 
  7.             byte[] filenamebyte =new byte[buffer.getInt()]; 
  8.             buffer.get(filenamebyte); 
  9.             fileName =new String(filenamebyte,"UTF-8"); 
  10.             fileSize = buffer.getLong(); 
  11.         } catch (IOException e) { 
  12.             e.printStackTrace(); 
  13.         } 
  14.     } 
  15.  
  16. private void parseBody(long size) { 
  17.         long read = 0L
  18.         long count = 8192
  19.         FileChannel fileChannel =null
  20.         try { 
  21.             fileChannel =new FileOutputStream(path + fileName) 
  22.                     .getChannel(); 
  23.             System.out.println(fileName); 
  24.             while (read < size) { 
  25.                 if (size - read <count
  26.                     count = size - read; 
  27.                 read += fileChannel 
  28.                         .transferFrom(clientChannel, 0 + read, count); 
  29.             } 
  30.  
  31.         } catch (IOException e) { 
  32.             e.printStackTrace(); 
  33.         }finally{ 
  34.             try { 
  35.                 fileChannel.close(); 
  36.             } catch (IOException e) { 
  37.                 e.printStackTrace(); 
  38.             } 
  39.         } 
  40.     } 

好了,希望对你有所帮助,有事去了。不行的话留下邮箱,我发送源码给你。

0 0