controller
@PostMapping("/uploadShardingFileLocal")
public AjaxResult uploadChunk(TchunkInfo chunk) {
chunk.setSliceNo(chunk.getSliceNo()+1);
MultipartFile file = chunk.getFile();
String filePath = RuoYiConfig.getUploadPath();
try
{
byte[] bytes = file.getBytes();
Path path = Paths.get(FileInfoUtils.generatePath(filePath, chunk));
Files.write(path, bytes);
if (chunk.getSliceNo().equals(chunk.getFileSlicesNum()))
{
String taskId = chunk.getTaskId();
String localFile = filePath + "/" + taskId + "/" + chunk.getFileName();
String folder = filePath + "/" + taskId;
FileInfoUtils.merge(localFile, folder, chunk.getFileName());
int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
String currentDir = StringUtils.substring(filePath, dirLastIndex);
Map<String,String> resultMap = new HashMap<>();
resultMap.put("fileUrl", serverConfig.getUrl() + Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + taskId + "/" + chunk.getFileName());
resultMap.put("fileName", chunk.getFileName());
return AjaxResult.success(resultMap);
}
} catch (IOException e)
{
e.printStackTrace();
return AjaxResult.error("上传失败!");
}
return AjaxResult.success();
}
FileInfoUtils
package com.ruoyi.api.common.fileupload;
import com.ruoyi.common.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.*;
public class FileInfoUtils {
private final static Logger logger = LoggerFactory.getLogger(FileInfoUtils.class);
public static String generatePath(String uploadFolder, TchunkInfo chunk) {
StringBuilder sb = new StringBuilder();
sb.append(uploadFolder).append("/").append(chunk.getTaskId());
if (!Files.isWritable(Paths.get(sb.toString())))
{
logger.info("path not exist,create path: {}", sb.toString());
try
{
Files.createDirectories(Paths.get(sb.toString()));
} catch (IOException e)
{
logger.error(e.getMessage(), e);
}
}
return sb.append("/").append(chunk.getFileName()).append("-").append(chunk.getSliceNo()).toString();
}
public static AjaxResult merge(String file, String folder, String filename) {
try
{
if (fileExists(file))
{
return AjaxResult.warn("文件已存在!");
} else
{
Files.createFile(Paths.get(file));
Files.list(Paths.get(folder)).filter(path -> !path.getFileName().toString().equals(filename)).sorted((o1, o2) -> {
String p1 = o1.getFileName().toString();
String p2 = o2.getFileName().toString();
int i1 = p1.lastIndexOf("-");
int i2 = p2.lastIndexOf("-");
return Integer.valueOf(p2.substring(i2)).compareTo(Integer.valueOf(p1.substring(i1)));
}).forEach(path -> {
try
{
Files.write(Paths.get(file), Files.readAllBytes(path), StandardOpenOption.APPEND);
Files.delete(path);
} catch (IOException e)
{
logger.error(e.getMessage(), e);
}
});
}
} catch (IOException e)
{
logger.error(e.getMessage(), e);
return AjaxResult.warn("合并失败!");
}
return AjaxResult.success();
}
public static boolean fileExists(String file) {
boolean fileExists = false;
Path path = Paths.get(file);
fileExists = Files.exists(path, new LinkOption[]{
LinkOption.NOFOLLOW_LINKS});
return fileExists;
}
}
TchunkInfo
package com.ruoyi.api.common.fileupload;
import org.springframework.web.multipart.MultipartFile;
import java.io.Serializable;
public class TchunkInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String taskId;
private Integer sliceNo;
private Long chunkSize;
private Long totalSize;
private String fileName;
private Integer fileSlicesNum;
private String type;
private transient MultipartFile file;
public Integer getSliceNo() {
return sliceNo;
}
public void setSliceNo(Integer sliceNo) {
this.sliceNo = sliceNo;
}
public Long getChunkSize() {
return chunkSize;
}
public void setChunkSize(Long chunkSize) {
this.chunkSize = chunkSize;
}
public Long getTotalSize() {
return totalSize;
}
public void setTotalSize(Long totalSize) {
this.totalSize = totalSize;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public Integer getFileSlicesNum() {
return fileSlicesNum;
}
public void setFileSlicesNum(Integer fileSlicesNum) {
this.fileSlicesNum = fileSlicesNum;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
}