|
|
|
@@ -148,70 +148,49 @@ public class DmResourcesController extends BaseController<DmResourcesQuery, DmRe |
|
|
|
private static final String PARAM_TYPE = "type"; |
|
|
|
|
|
|
|
|
|
|
|
private MultipartFile[] completedChunks; // 用于跟踪已上传完毕的块 |
|
|
|
|
|
|
|
@PostMapping("/chunkUpload") |
|
|
|
public AjaxResult uploadChunk( |
|
|
|
@RequestParam("file") MultipartFile file, |
|
|
|
@RequestParam("chunkIndex") int chunkIndex, |
|
|
|
@RequestParam("chunkCount") int totalChunks |
|
|
|
@RequestParam("chunkCount") int totalChunks, |
|
|
|
@RequestParam("uploadUuid") String uuid |
|
|
|
) throws IOException { |
|
|
|
if (completedChunks == null) { |
|
|
|
completedChunks = new MultipartFile[totalChunks]; |
|
|
|
List<byte[]> bytesList = new ArrayList<>(); |
|
|
|
String key = "saas:upload:chunks:" + uuid; |
|
|
|
if (RedisUtil.existed(key)){ |
|
|
|
bytesList = RedisUtil.getBytesList(key); |
|
|
|
} |
|
|
|
logger.info("completeChunks size:{}", completedChunks.length); |
|
|
|
|
|
|
|
logger.info("第{}/{}块上传成功", chunkIndex, totalChunks); |
|
|
|
logger.info("第{}/{}块上传成功", chunkIndex + 1, totalChunks); |
|
|
|
|
|
|
|
completedChunks[chunkIndex] = file; // 标记当前块已上传完毕 |
|
|
|
String uuid = IdUtil.randomUUID(); |
|
|
|
RedisUtil.setBytesList(key+":"+chunkIndex, file.getBytes()); |
|
|
|
// String uuid = IdUtil.randomUUID(); |
|
|
|
|
|
|
|
// 如果所有块都上传完毕,开始合并 |
|
|
|
if (allChunksUploaded(completedChunks)) { |
|
|
|
if (RedisUtil.getListCount(key) == totalChunks) { |
|
|
|
logger.info("所有块上传完毕,开始合并..."); |
|
|
|
String extension = FileTypeUtil.getExtension(file); |
|
|
|
return executeUpload(mergeChunks(completedChunks), extension, uuid); |
|
|
|
return executeUpload(mergeChunks(bytesList), extension, uuid); |
|
|
|
} |
|
|
|
logger.info("上传完毕"); |
|
|
|
|
|
|
|
return AjaxResult.error("上传文件异常,请联系管理员!"); |
|
|
|
} |
|
|
|
|
|
|
|
private boolean allChunksUploaded(MultipartFile[] completedChunks) { |
|
|
|
for (MultipartFile completed : completedChunks) { |
|
|
|
if (null == completed) { |
|
|
|
return false; // 如果有任何一个块没有上传完毕,返回false |
|
|
|
} |
|
|
|
private MultipartFile mergeChunks(List<byte[]> byteArrays) { |
|
|
|
// 合并所有分片的byte数组为一个byte数组 |
|
|
|
int totalSize = byteArrays.stream().mapToInt(byteArray -> byteArray.length).sum(); |
|
|
|
byte[] mergedBytes = new byte[totalSize]; |
|
|
|
int currentIndex = 0; |
|
|
|
for (byte[] bytes : byteArrays) { |
|
|
|
System.arraycopy(bytes, 0, mergedBytes, currentIndex, bytes.length); |
|
|
|
currentIndex += bytes.length; |
|
|
|
} |
|
|
|
return true; // 所有块都上传完毕 |
|
|
|
} |
|
|
|
|
|
|
|
private MultipartFile mergeChunks(MultipartFile[] multipartFiles) { |
|
|
|
List<byte[]> byteArrays = null; |
|
|
|
try { |
|
|
|
byteArrays = new ArrayList<>(); |
|
|
|
|
|
|
|
for (MultipartFile part : multipartFiles) { |
|
|
|
// 从每个分片的MultipartFile中读取数据并存储到byte数组中 |
|
|
|
byte[] bytes = part.getBytes(); |
|
|
|
byteArrays.add(bytes); |
|
|
|
} |
|
|
|
|
|
|
|
// 合并所有分片的byte数组为一个byte数组 |
|
|
|
int totalSize = byteArrays.stream().mapToInt(byteArray -> byteArray.length).sum(); |
|
|
|
byte[] mergedBytes = new byte[totalSize]; |
|
|
|
int currentIndex = 0; |
|
|
|
for (byte[] bytes : byteArrays) { |
|
|
|
System.arraycopy(bytes, 0, mergedBytes, currentIndex, bytes.length); |
|
|
|
currentIndex += bytes.length; |
|
|
|
} |
|
|
|
completedChunks = null; |
|
|
|
// 创建一个新的MultipartFile对象,代表合并后的文件 |
|
|
|
return new ByteArrayMultipartFile(mergedBytes); |
|
|
|
|
|
|
|
// 创建一个新的MultipartFile对象,代表合并后的文件 |
|
|
|
return new ByteArrayMultipartFile(mergedBytes); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
|