android java Socket多文件发送

android java Socket多文件发送       
 

     

  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.             } 
for (int i = 0;i < files.length; i++) {
                byte[] namebyte = files[i].getName().getBytes("UTF-8");
                long size = files[i].length();
                int nameLength = namebyte.length;
                fileChannel = new FileInputStream(files[i]).getChannel();
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                buffer.clear();
                buffer.putInt(4+8+nameLength);
                buffer.putInt(nameLength);
                buffer.put(namebyte);
                buffer.putLong(size);
                buffer.flip();
                while(buffer.hasRemaining()){
                    sc.write(buffer);
                }
                long count = 1024*1024;
                long read = 0L;
                while(read < size){
                    if(size - read < count)
                        count = size - read;
                    read += fileChannel.transferTo(0+read, count, sc);
                    System.out.println("read:"+read);
                }
                fileChannel.close();
                if(i < files.length -1){
                    sc.write(ByteBuffer.wrap(new byte[]{1}));
                    System.out.println(1);
                }
                else
                    sc.write(ByteBuffer.wrap(new byte[]{0}));
            }


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

   

  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.     } 
private void parseHead(int headlength) {
        buffer = ByteBuffer.allocate(headlength);
        try {
            while(buffer.position() < buffer.capacity())
              clientChannel.read(buffer);
            buffer.flip();
            byte[] filenamebyte = new byte[buffer.getInt()];
            buffer.get(filenamebyte);
            fileName = new String(filenamebyte,"UTF-8");
            fileSize = buffer.getLong();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

private void parseBody(long size) {
        long read = 0L;
        long count = 8192;
        FileChannel fileChannel = null;
        try {
            fileChannel = new FileOutputStream(path + fileName)
                    .getChannel();
            System.out.println(fileName);
            while (read < size) {
                if (size - read < count)
                    count = size - read;
                read += fileChannel
                        .transferFrom(clientChannel, 0 + read, count);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fileChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值