mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
update:重构部分上传接口逻辑
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
package cn.hellohao.utils;
|
||||
|
||||
import cn.hellohao.pojo.Chunk;
|
||||
import cn.hellohao.pojo.Msg;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SetFiles {
|
||||
// 转换文件方法
|
||||
@@ -78,7 +85,73 @@ public class SetFiles {
|
||||
return fileSizeString;
|
||||
}
|
||||
|
||||
public static String generatePath(String uploadFolder, Chunk chunk) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(uploadFolder+File.separator).append("/").append(chunk.getIdentifier());
|
||||
if (!Files.isWritable(Paths.get(sb.toString()))) {
|
||||
System.err.println("path not exist,create path");
|
||||
System.err.println(sb.toString());
|
||||
try {
|
||||
Files.createDirectories(Paths.get(sb.toString()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return sb.append("/")
|
||||
.append(chunk.getFilename())
|
||||
.append("-")
|
||||
.append(chunk.getChunkNumber()).toString();
|
||||
}
|
||||
|
||||
public static Msg merge(String targetFile, String folder, String filename) {
|
||||
Msg msg = new Msg();
|
||||
try {
|
||||
final File file = new File(targetFile);
|
||||
if (file.exists()){
|
||||
file.delete();
|
||||
}
|
||||
Files.createFile(Paths.get(targetFile));
|
||||
} catch (IOException e) {
|
||||
System.out.println("这是一个无伤大雅的错:IOException");
|
||||
e.printStackTrace();
|
||||
}
|
||||
try (Stream<Path> stream = Files.list(Paths.get(folder))) {
|
||||
stream.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(targetFile), Files.readAllBytes(path), StandardOpenOption.APPEND);
|
||||
//合并后删除该块
|
||||
Files.delete(path);
|
||||
} catch (IOException e) {
|
||||
msg.setCode("500");
|
||||
msg.setInfo("合并文件时发生了错误");
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
msg.setCode("500");
|
||||
msg.setInfo("合并文件时发生了错误");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static void delFileFolder(File file){
|
||||
if (file.isDirectory()){
|
||||
File[] files = file.listFiles();
|
||||
for (File in: files) {
|
||||
delFileFolder(in);
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user