前一阵子做一个android的类似飞鸽的东西,用到了Socket发送单个或者多个文件,在此分享出来,还望对大家有所帮助。

发送方核心代码:

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


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

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

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

更多相关文章

  1. Android 2.3.3 安卓系统 源代码 在Eclipse查看方法
  2. Android proguard代码混淆
  3. Android 交叉编译 Linux 可执行文件
  4. Android 工程中的 R.java 文件丢失后如何重新生成
  5. 【Android Developers Training】 25. 保存文件
  6. Android本地数据存储之.txt文件存储读写

随机推荐

  1. 全局共享变量(Android)
  2. SqliteDatabase
  3. 自定义 RadioButton 图片
  4. Android经典的大牛博客推荐
  5. Android实现开机自启动无效问题
  6. Android(安卓)Wi-Fi connect & auto conn
  7. 关于Android使用Google Map Android(安卓
  8. android添加删除桌面快捷方式
  9. Android编译时主要瓶颈分析
  10. SqliteDatabase