|
|
|
@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.xueyi.common.core.utils.core.IdUtil; |
|
|
|
import com.xueyi.common.core.utils.file.FileTypeUtil; |
|
|
|
import com.xueyi.common.core.utils.file.FileUtil; |
|
|
|
import com.xueyi.common.core.utils.file.MimeTypeUtil; |
|
|
|
import com.xueyi.common.core.web.result.AjaxResult; |
|
|
|
import com.xueyi.common.core.web.result.R; |
|
|
|
@@ -26,6 +27,7 @@ import com.xueyi.system.resource.service.IDmResourcesService; |
|
|
|
import com.xueyi.system.resource.service.impl.FaceServiceImpl; |
|
|
|
import com.xueyi.system.utils.common.ImageUtil; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.mock.web.MockMultipartFile; |
|
|
|
import org.springframework.validation.annotation.Validated; |
|
|
|
import org.springframework.web.bind.annotation.DeleteMapping; |
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
@@ -38,10 +40,11 @@ import org.springframework.web.bind.annotation.RequestParam; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.Serializable; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
@@ -152,28 +155,44 @@ public class DmResourcesController extends BaseController<DmResourcesQuery, DmRe |
|
|
|
@PostMapping("/chunkUpload") |
|
|
|
public AjaxResult uploadChunk( |
|
|
|
@RequestParam("file") MultipartFile file, |
|
|
|
@RequestParam("chunkIndex") int chunkIndex, |
|
|
|
@RequestParam("chunkCount") int totalChunks, |
|
|
|
@RequestParam("chunkIndex") int chunk, |
|
|
|
@RequestParam("chunkCount") int chunks, |
|
|
|
@RequestParam("uploadUuid") String uuid |
|
|
|
) throws IOException { |
|
|
|
List<byte[]> bytesList = new ArrayList<>(); |
|
|
|
String key = "saas:upload:chunks:" + uuid; |
|
|
|
|
|
|
|
String folder = "/home/xueyi/upload/temp/"; |
|
|
|
File dir = new File(folder); |
|
|
|
if (!dir.exists()) { |
|
|
|
dir.mkdirs(); |
|
|
|
} |
|
|
|
|
|
|
|
logger.info("第{}/{}块上传成功", chunkIndex + 1, totalChunks); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
|
|
|
|
RedisUtil.setBytesList(key, file.getBytes()); |
|
|
|
// String uuid = IdUtil.randomUUID(); |
|
|
|
|
|
|
|
logger.info("redis count:{}",RedisUtil.getListCount(key)); |
|
|
|
// 如果所有块都上传完毕,开始合并 |
|
|
|
if (RedisUtil.getListCount(key) == totalChunks) { |
|
|
|
logger.info("所有块上传完毕,开始合并..."); |
|
|
|
String extension = FileTypeUtil.getExtension(file); |
|
|
|
if (RedisUtil.existed(key)){ |
|
|
|
bytesList = RedisUtil.getBytesList(key).stream().map(ss->ss.getBytes()).collect(Collectors.toList()); |
|
|
|
String path = folder + file.getOriginalFilename() + "_" + chunk + ".part"; |
|
|
|
try { |
|
|
|
file.transferTo(new File(path)); |
|
|
|
RedisUtil.increment("saas:upload:chunk:"+uuid,1); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return AjaxResult.error("文件保存失败"); |
|
|
|
} |
|
|
|
if ((Long)RedisUtil.getVal("saas:upload:chunk:"+uuid) == chunks) { |
|
|
|
String name = file.getOriginalFilename(); |
|
|
|
// 合并所有分片 |
|
|
|
File[] content = new File[chunks]; |
|
|
|
for (int i = 0; i < chunks; i++) { |
|
|
|
content[i] = new File(folder + name + "_" + i + ".part"); |
|
|
|
} |
|
|
|
File destFile = new File(folder + name); |
|
|
|
FileUtil.mergeFile(content, destFile); |
|
|
|
// 删除分片文件 |
|
|
|
for (int i = 0; i < chunks; i++) { |
|
|
|
new File(folder + name + "_" + i + ".part").delete(); |
|
|
|
} |
|
|
|
return executeUpload(mergeChunks(bytesList), extension, uuid); |
|
|
|
MultipartFile multipartFile = new MockMultipartFile(name, new FileInputStream(destFile)); |
|
|
|
|
|
|
|
RedisUtil.expire("saas:upload:chunk:"+uuid, 5); |
|
|
|
return executeUpload(multipartFile, FileTypeUtil.getExtension(file), uuid); |
|
|
|
} |
|
|
|
|
|
|
|
return AjaxResult.success("上传文件成功!"); |
|
|
|
|