要利用socket向客户端传容量比较大的文件,比如大的图片,大的视频文件等,有什么好的优化方案吗?
把文件切割成小片的话,怎么切割,有具体的实例吗?谢谢大家帮助。

解决方案 »

  1.   

    文件切割的程序:http://www.99inf.net/SoftwareDev/Java/54606.htm利用多线程并发读写。效率可能会快一点。
      

  2.   

    我也来个分割文件的程序,供参考,合并的未贴:public class FileDivision {
    // 单个文件大小
    private static final long SPLIT_SIZE = 5 * 1024 * 1024; /**
     *分割文件
     * 
     * @author Jason,Wang
     * @param filePath
     *            要分割的文件路径
     * @return int 分割的数量
     */
    public static int split(String filePath) {
    // 子文件数量
    int number = 1;
    // 文件通道
    FileChannel in = null;
    try {
    in = new FileInputStream(filePath).getChannel();
    // 文件长度
    long fileLength = in.size();
    long position = 0;
    while (position < fileLength) {
    // 子文件
    FileChannel out = new FileOutputStream(filePath + (number++))
    .getChannel();
    in.transferTo(position, SPLIT_SIZE, out);
    out.close();
    position += SPLIT_SIZE;
    }
    } catch (Exception e) {
    e.printStackTrace();
    number = 1;
    } finally {
    if (in != null)
    try {
    in.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return number;
    }
    }
      

  3.   

    http://www.99inf.net/SoftwareDev/Java/54606.htm
    利用多线程技术也能够提高效率
    数据已经读入内存中,因为要对这些数据进行一下处理,然后再传图片、视频
    这个文件分割技术的代码也许会对你有些帮助
    public class FileDivision { // 单个文件大小  
    private static final long SPLIT_SIZE = 5 * 1024 * 1024;
     /**  *分割文件 * * @author Jason,Wang * @param filePath * 要分割的文件路径 * @return int 分割的数量 */ 
     public static int split(String filePath) 
    { // 子文件数量  int number = 1; 
    // 文件通道  FileChannel in = null;
     try { in = new FileInputStream(filePath).getChannel(); 
    // 文件长度  long fileLength = in.size(); 
    long position = 0; while (position < fileLength) 
    { // 子文件  FileChannel out = new FileOutputStream(filePath + (number++)) .getChannel();
     in.transferTo(position, SPLIT_SIZE, out); out.close(); position += SPLIT_SIZE; } } 
    catch (Exception e) { e.printStackTrace(); number = 1; } 
    finally { if (in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return number; } }
      

  4.   

    4楼的推荐文章应该提够一点思路,改改应该能够运用到socket上来。
    1.分割文件
    2.分多个线程,每个线程传输一个分割快到客户端
    3.客户端等所有接受线程都完成的时候在合并文件