我正在尝试" chunk"上升图像的字节。这将允许我以字节数组上传大图像。我将当前存储的图像存储为一个大字节[]。我想将字节数组拆分为byte []' s,每个都精确到5 MB。
答案 0 :(得分:1)
您可以使用copyOfRange:
T[] copyOfRange (T[] original,
int from,
int to);
在你的情况下,像这样:
Byte[] copyOfRange (original,
0,
5000000);
确保计算偏移量:
class test {
// this is just for dummy data
public static byte[] getTestBytes() {
byte[] largeByteArray = new byte[50_000_000];
for(int i = 0; i < 50_000_000; i ++) {
largeByteArray[i] = 0;
}
return largeByteArray;
}
// this method splits your byte array into small portions
// and returns a list with those portions
public static List<byte[]> byteToPortions(byte[] largeByteArray) {
// create a list to keep the portions
List<byte[]> byteArrayPortions = new ArrayList<>();
// 5mb is about 5.000.000 bytes
int sizePerPortion = 5_000_000;
int offset = 0;
// split the array
while(offset < largeByteArray.length) {
// into 5 mb portions
byte[] portion = Arrays.copyOfRange(largeByteArray, offset, offset + sizePerPortion);
// update the offset to increment the copied area
offset += sizePerPortion;
// add the byte array portions to the list
byteArrayPortions.add(portion);
}
// return portions
return byteArrayPortions;
}
// create your byte array, and split it to portions
public static void main(String[] args) {
byte[] largeByteArray = getTestBytes();
List<byte[]> portions = byteToPortions(largeByteArray);
// work with your portions
}
}
很酷:价值to
不必须是数组中的索引,它会为您检查而不会出错并复制对目标数组有效的子集。
答案 1 :(得分:1)
divideArray(common.fullyReadFileToBytes(wallpaperDirectory), 5 * 1024 * 1024)
通过
调用它$("input[type='file']").val("");