vue+spring 断点续传(二)
本文主要讲述断点续传的后端实现,后端实现的要点是分片的控制和并发的考虑
提示:以下是本篇文章正文内容,下面案例可供参考
一、环境
spring 2.5.0
二、后端实现
1.bean对象
FileInfo对象主要用于记录文件的信息,getter和setter请自行实现
public class FileInfo {
private String hashCode;
private String fileName;
private int fileSize;
private int totalIndex;
private Set<Integer> hasUploadShard = new HashSet<>();
private Set<Integer> noUploadShard = new HashSet<>();
}
shardInfo对象主要用于记录分片的信息,getter和setter请自行实现
public class ShardInfo {
/**
* 文件hash值
*/
private String hashCode;
/**
* 分片索引
*/
private Integer shardIndex;
/**
* 分片的二进制数据
*/
private MultipartFile shard;
}
2.逻辑实现
代码如下(示例):逻辑代码仅可作为参考,还有很多不足之处,比如文件和分片的信息未进行落库、长时间未合并的分片的处理逻辑还未实现、IO合并方式的可靠性还未进行验证…
@RestController
@CrossOrigin(origins = "*")
public class upload {
private static final String savePath = "C:\\Users\\admin\\Desktop\\qianduan";
private static Map<String, FileInfo> fileMap = new ConcurrentHashMap<>();
@PostMapping("file/upload/shard")
public Map<String, Object> fileUploadShard(MultipartFile shard, ShardInfo shardInfo) throws Exception {
HashMap<String, Object> map = new HashMap<>();
shardInfo.setShard(shard);
System.out.println(shardInfo.getShardIndex());
try {
// 判断内存中是否已有文件,如若没有抛出异常
FileInfo fileInfo = checkMemoryAndUpdate(shardInfo);
// 判断分片是否已经创建
boolean contains = fileInfo.getHasUploadShard().contains(shardInfo.getShardIndex());
if (contains) {
map.put("status", "分片已创建");
map.put("status", 0);
return map;
}
// 创建分片
createShardAndUpdate(fileInfo, shardInfo);
// 封装结果
// 0:分片正常上传
map.put("status", 0);
map.put("fileInfo", fileInfo);
// 判断并合并分片
mergeShard(fileInfo);
return map;
} catch (Exception e) {
// 1:分片上传出现异常
map.put("status", 1);
return map;
}
}
@PostMapping("file/status")
public Map<String, Object> getFileStatus(FileInfo fileInfo) {
HashMap<String, Object> res = new HashMap<>();
FileInfo info = fileMap.get(fileInfo.getHashCode());
if (info == null) {
// 文件首次上传
info = new FileInfo();
info.setFileName(fileInfo.getFileName());
info.setFileSize(fileInfo.getFileSize());
info.setHashCode(fileInfo.getHashCode());
info.setTotalIndex(fileInfo.getTotalIndex());
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < info.getTotalIndex(); i++) {
set.add(i);
}
info.setNoUploadShard(set);
fileMap.put(info.getHashCode(), info);
}
// 文件有记录
res.put("fileInfo", info);
return res;
}
/**
* 获取内存中文件信息
*
* @return
*/
@GetMapping("file/list")
public Map<String, FileInfo> getFileList() {
return fileMap;
}
// 判断并合并分片
private void mergeShard(FileInfo fileInfo) throws IOException {
if (fileInfo.getNoUploadShard().size() == 0) {
// 分片文件名
ArrayList<String> shardNames = new ArrayList<>();
// 创建文件名和文件
String fileName = fileInfo.getHashCode() + getExtension(fileInfo.getFileName());
File overAllFile = new File(savePath + "\\" + fileName);
// 分片文件名
for (int i = 0; i < fileInfo.getTotalIndex(); i++) {
String shardName = fileInfo.getHashCode() + "_" + i + ".blob";
shardNames.add(shardName);
}
// 获取输出流
FileOutputStream out = new FileOutputStream(overAllFile);
byte[] buf = new byte[1024];
// 循环copy
shardNames.forEach(val -> {
File shardFile = new File(savePath + "\\" + val);
int len = 0;
try {
FileInputStream in = new FileInputStream(shardFile);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.flush();
} catch (Exception e) {
overAllFile.delete();
e.printStackTrace();
}
});
// 关闭
out.close();
// 成功后删除分片
shardNames.forEach(val -> {
File shardFile = new File(savePath + "\\" + val);
shardFile.delete();
});
}
}
/**
* 获取文件后缀
*
* @param fileName
* @return
*/
private String getExtension(String fileName) {
if (fileName == null) {
return null;
}
int extensionPos = fileName.lastIndexOf(46);
int lastUnixPos = fileName.lastIndexOf(47);
int lastWindowsPos = fileName.lastIndexOf(92);
int lastSeparator = Math.max(lastUnixPos, lastWindowsPos);
if (lastSeparator < extensionPos) {
//
return "." + fileName.substring(extensionPos + 1);
} else {
return "";
}
}
// 创建分片并更新信息
private void createShardAndUpdate(FileInfo fileInfo, ShardInfo shardInfo) throws IOException {
File file1 = new File(savePath + "\\" + fileInfo.getHashCode() + "_" + shardInfo.getShardIndex() + ".blob");
FileCopyUtils.copy(shardInfo.getShard().getInputStream(), new FileOutputStream(file1));
// 更新分片数据
updateUploadShard(fileInfo, shardInfo.getShardIndex());
}
// 检查内存是否有文件记录,没有则添加
public FileInfo checkMemoryAndUpdate(ShardInfo fileShard) throws Exception {
FileInfo info = fileMap.get(fileShard.getHashCode());
if (info == null) {
throw new Exception("文件不存在");
}
return info;
}
private void updateUploadShard(FileInfo fileInfo, Integer shardIndex) {
// 添加已经上传的shardIndex
fileInfo.getHasUploadShard().add(shardIndex);
// 更新未上传的shardIndex
setNoUploadShard(fileInfo);
}
private void setNoUploadShard(FileInfo fileInfo) {
Set<Integer> uploadShard = fileInfo.getHasUploadShard();
Set<Integer> integers = new HashSet<>();
for (int i = 0; i < fileInfo.getTotalIndex(); i++) {
boolean contains = uploadShard.contains(i);
// 如若不含
if (!contains) {
integers.add(i);
}
}
fileInfo.setNoUploadShard(integers);
}
}
总结
加油噢!