国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 大文件切割與合并的實現代碼

2019-10-21 21:36:00
字體:
來源:轉載
供稿:網友

前言:

由于公司的業務,硬生生的把ios開發的我,掰成了android!關于上傳文件的需求處理,做了一個Java的簡單封裝 DocumentManagement 。其中集成了,檢測文件,MD5加密,Base64加密/解碼,針對文件Base64加密處理,獲取文件后戳,切割文件,合并文件等方法。

親測可切割與合并有效:視頻、mp3、jpg、apk!還有很多沒測,講道理應該是都可以的。合并效果如圖:

Android,大文件,切割,合并,代碼

好了不扯皮了,直接上代碼!注:以下代碼僅供參考,如有想法請留言告知 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下載地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽源市| 榆中县| 鲁甸县| 毕节市| 凉山| 西吉县| 郓城县| 喀喇沁旗| 绥滨县| 涿州市| 卢湾区| 江阴市| 邵武市| 磐安县| 博兴县| 盘山县| 湾仔区| 南通市| 南皮县| 墨脱县| 海南省| 南陵县| 淅川县| 内丘县| 临猗县| 新野县| 鲁山县| 营山县| 靖边县| 桐城市| 松原市| 博湖县| 元朗区| 宜春市| 津南区| 武汉市| 上杭县| 齐齐哈尔市| 息烽县| 和田县| 静宁县|