Android 大文件切割与合并的实现代码

所属分类: 软件编程 / Android 阅读数: 43
收藏 0 赞 0 分享

前言:

由于公司的业务,硬生生的把ios开发的我,掰成了android!关于上传文件的需求处理,做了一个Java的简单封装 DocumentManagement 。其中集成了,检测文件,MD5加密,Base64加密/解码,针对文件Base64加密处理,获取文件后戳,切割文件,合并文件等方法。

亲测可切割与合并有效:视频、mp3、jpg、apk!还有很多没测,讲道理应该是都可以的。合并效果如图:

好了不扯皮了,直接上代码!注:以下代码仅供参考,如有想法请留言告知 DocumentManagement 使用方法如下:

//文件
              File file = new File(strPath);

              documentManagement.log("开始——汪汪汪汪");
              //切割文件
              documentManagement.getSplitFile(file,1*1024*1024 );

              //合并文件
              String merFileName = "gsplay";//自定义合并文件名字
              //创建合并文件路径
              String filePath = Environment.getExternalStorageDirectory().getPath()+"/"+merFileName;

              documentManagement.merge(filePath,file,1*1024*1024);
              documentManagement.log("结束——汪汪汪汪");

Java获取文件后缀

/**
   * 获取文件后缀名 例如:.mp4 /.jpg /.apk
   * @param file 指定文件
   * @return String 文件后缀名
   */
  public static String suffixName (File file){
    String fileName=file.getName();
    String fileTyle=fileName.substring(fileName.lastIndexOf("."),fileName.length());
    return fileTyle;
  }

文件按设定的大小进行切割

/**
   * 文件分割方法
   * @param targetFile 分割的文件
   * @param cutSize 分割文件的大小
   * @return int 文件切割的个数
   */
  public static int getSplitFile(File targetFile ,long cutSize ) {

    //计算切割文件大小
    int count = targetFile.length() % cutSize == 0 ? (int) (targetFile.length() / cutSize) :
        (int) (targetFile.length() / cutSize + 1);

    RandomAccessFile raf = null;
    try {
      //获取目标文件 预分配文件所占的空间 在磁盘中创建一个指定大小的文件  r 是只读
      raf = new RandomAccessFile(targetFile, "r");
      long length = raf.length();//文件的总长度
      long maxSize = length / count;//文件切片后的长度
      long offSet = 0L;//初始化偏移量

      for (int i = 0; i < count - 1; i++) { //最后一片单独处理
        long begin = offSet;
        long end = (i + 1) * maxSize;
        offSet = getWrite(targetFile.getAbsolutePath(), i, begin, end);
      }
      if (length - offSet > 0) {
        getWrite(targetFile.getAbsolutePath(), count-1, offSet, length);
      }

    } catch (FileNotFoundException e) {
//      System.out.println("没有找到文件");
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        raf.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return count;
  }
  /**
   * 指定文件每一份的边界,写入不同文件中
   * @param file 源文件地址
   * @param index 源文件的顺序标识
   * @param begin 开始指针的位置
   * @param end 结束指针的位置
   * @return long
   */
  public static long getWrite(String file,int index,long begin,long end ){

    long endPointer = 0L;

    String a=file.split(suffixName(new File(file)))[0];

    try {
      //申明文件切割后的文件磁盘
      RandomAccessFile in = new RandomAccessFile(new File(file), "r");
      //定义一个可读,可写的文件并且后缀名为.tmp的二进制文件
      //读取切片文件
      File mFile = new File(a + "_" + index + ".tmp");
      //如果存在
      if (!isFileExist(mFile)) {
        RandomAccessFile out = new RandomAccessFile(mFile, "rw");
        //申明具体每一文件的字节数组
        byte[] b = new byte[1024];
        int n = 0;
        //从指定位置读取文件字节流
        in.seek(begin);
        //判断文件流读取的边界
        while ((n = in.read(b)) != -1 && in.getFilePointer() <= end) {
          //从指定每一份文件的范围,写入不同的文件
          out.write(b, 0, n);
        }

        //定义当前读取文件的指针
        endPointer = in.getFilePointer();
        //关闭输入流
        in.close();
        //关闭输出流
        out.close();
      }else {
        //不存在

      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return endPointer - 1024;
  }

文件合并

/**
   * 文件合并
   * @param fileName 指定合并文件
   * @param targetFile 分割前的文件
   * @param cutSize 分割文件的大小
   */
  public static void merge(String fileName,File targetFile ,long cutSize) {


    int tempCount = targetFile.length() % cutSize == 0 ? (int) (targetFile.length() / cutSize) :
        (int) (targetFile.length() / cutSize + 1);
    //文件名
    String a=targetFile.getAbsolutePath().split(suffixName(targetFile))[0];

    RandomAccessFile raf = null;
    try {
      //申明随机读取文件RandomAccessFile
      raf = new RandomAccessFile(new File(fileName+suffixName(targetFile)), "rw");
      //开始合并文件,对应切片的二进制文件
      for (int i = 0; i < tempCount; i++) {
        //读取切片文件
        File mFile = new File(a + "_" + i + ".tmp");
        //
        RandomAccessFile reader = new RandomAccessFile(mFile, "r");
        byte[] b = new byte[1024];
        int n = 0;
         //先读后写
         while ((n = reader.read(b)) != -1) {//读
           raf.write(b, 0, n);//写
         }
         //合并后删除文件
        isDeleteFile(mFile);
         //日志
        log(mFile.toString());
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        raf.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

DocumentManagement_Dome_Git下载地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多