Skip to content

Commit

Permalink
refactor-fileServer-更换同步save方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Ning19230223 committed Oct 25, 2024
1 parent 8866a6e commit 352ebf1
Showing 1 changed file with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.file.FileSystem;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.UUID;
Expand Down Expand Up @@ -97,15 +99,35 @@ public String save(File file) {
}

// 保存文件
// public String save(File file, String assignName) {
// String saveId = generateBsyUid();
// String saveFileNameUid = suffixFileNameWithN(saveId);
// String saveFileContextUid = suffixFileNameWithO(saveId);
// FileSystem fileSystem = vertx.fileSystem();
// // 写入文件名
// fileSystem.writeFile(getUploadPath() + saveFileNameUid, Buffer.buffer(assignName));
// fileSystem.copy(file.getAbsolutePath(), getUploadPath() + saveFileContextUid);
// fileSystem.delete(file.getAbsolutePath());
// return "%s@%s".formatted(saveId, assignName);
// }

// 同步save方法
public String save(File file, String assignName) {
String saveId = generateBsyUid();
String saveFileNameUid = suffixFileNameWithN(saveId);
String saveFileContextUid = suffixFileNameWithO(saveId);
FileSystem fileSystem = vertx.fileSystem();
// 写入文件名
fileSystem.writeFile(getUploadPath() + saveFileNameUid, Buffer.buffer(assignName));
fileSystem.copy(file.getAbsolutePath(), getUploadPath() + saveFileContextUid);
fileSystem.delete(file.getAbsolutePath());
try (BufferedWriter writer = new BufferedWriter(new FileWriter(getUploadPath() + saveFileNameUid))) {
writer.write(assignName);
} catch (IOException e) {
e.printStackTrace();
}
Path sourcePath = Paths.get(file.getAbsolutePath());
Path targetPath = Paths.get(getUploadPath() + saveFileContextUid);
try {
Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
return "%s@%s".formatted(saveId, assignName);
}

Expand Down

0 comments on commit 352ebf1

Please sign in to comment.