Android 大檔案切割與合併
前言:
由於公司的業務,硬生生的把ios開發的我,掰成了android!關於上傳檔案的需求處理,做了一個Java的簡單封裝 DocumentManagement 。其中集成了,檢測檔案,MD5加密,Base64加密/解碼,針對檔案Base64加密處理,獲取檔案後戳,切割檔案,合併檔案等方法。
親測可切割與合併有效:視訊、mp3、jpg、apk!還有很多沒測,講道理應該是都可以的。合併效果如圖:

0025EFD3E1691807BA4BBC2A5E8CA33F.png
好了不扯皮了,直接上程式碼!注:以下程式碼僅供參考,如有想法請留言告知
- 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(); } } }