場景:上傳文件較大,把存放文件內容byte數組拆分成小的。下載的時候按照順序合并。
起初覺得挺麻煩的,寫完覺得挺簡單。
切割:
/*** 拆分byte數組* * @param bytes* 要拆分的數組* @param size* 要按幾個組成一份* @return*/public byte[][] splitBytes(byte[] bytes, int size) {double splitLength = Double.parseDouble(size + "");int arrayLength = (int) Math.ceil(bytes.length / splitLength);byte[][] result = new byte[arrayLength][];int from, to;for (int i = 0; i < arrayLength; i++) {from = (int) (i * splitLength);to = (int) (from + splitLength);if (to > bytes.length)to = bytes.length;result[i] = Arrays.copyOfRange(bytes, from, to);}return result;}
合并: common lang3
ArrayUtils.addAll();