修改重复文件上传占用资源问题

master
whb 8 months ago
parent d6d420ac15
commit b109048649

@ -76,11 +76,17 @@ public class JupyterhubController {
@GetMapping("/fileExistState") @GetMapping("/fileExistState")
@AnonymousAccess @AnonymousAccess
public ResultEntity fileExistState(@ApiParam("案例名") String caseName) { public ResultEntity fileExistState(@ApiParam("案例名") String caseName) {
if (caseName == null)
{
return null;
}
//存储文件路径 //存储文件路径
String path = "/etc/jupyterhub/data/"; String path = "/etc/jupyterhub/data/";
//etc/jupyterhub/data/xxx/ // /etc/jupyterhub/data/caseName/
String newCreatFile = path + caseName+"/"; String newCreatFile = path + caseName+"/";
@ -92,9 +98,11 @@ public class JupyterhubController {
} }
//判断有无ipynb文件进行迁移 //判断有无ipynb文件进行迁移
// 使用File类表示这个文件 // 使用File类表示这个文件
///etc/jupyterhub/data/caseName.ipynb
String fileResource = path + caseName + ".ipynb"; String fileResource = path + caseName + ".ipynb";
File myFile = new File(fileResource); File myFile = new File(fileResource);
System.out.println("222222222222"+myFile); System.out.println("222222222222"+myFile);
@ -158,27 +166,36 @@ public class JupyterhubController {
//判断文件是否存在与文件夹里 再文件夹里就返回不存在就返回false //判断文件是否存在与文件夹里 再文件夹里就返回不存在就返回false
// return new ResultEntity<>(HttpStatus.BAD_REQUEST, "ipynb文件不存在", false); // return new ResultEntity<>(HttpStatus.BAD_REQUEST, "ipynb文件不存在", false);
///etc/jupyterhub/data/xxx/ // /etc/jupyterhub/data/caseName/
// /etc/jupyterhub/data/caseName/caseName.ipynb
String pathIpynb = newCreatFile + caseName + ".ipynb"; String pathIpynb = newCreatFile + caseName + ".ipynb";
System.out.println("3333333"+pathIpynb); System.out.println("3333333"+pathIpynb);
File file1 = new File(pathIpynb); File file1 = new File(pathIpynb);
if (!file1.exists()) { if (!file1.exists()) {
//判断数据集是否存在 //判断数据集是否存在
System.out.println("判断数据集是否存在123456789==="+pathIpynb);
String sonPath = newCreatFile + "/data/"; String sonPath = newCreatFile + "/data/";
if (new File(sonPath).exists()) { if (new File(sonPath).exists()) {
return new ResultEntity<>(HttpStatus.OK, "数据存在!", true); return new ResultEntity<>(HttpStatus.OK, "数据存在!", true);
}else { }else {
return new ResultEntity<>(HttpStatus.OK, "Ipynb文件不存在!",false); return new ResultEntity<>(HttpStatus.OK, "数据不存在!",false);
} }
}else {
String sonPath = newCreatFile + "/data/";
if (new File(sonPath).exists()) {
return new ResultEntity<>(HttpStatus.OK, "数据存在!", true);
}else {
return new ResultEntity<>(HttpStatus.OK, "数据不存在!",false);
}
} }
return new ResultEntity<>(HttpStatus.OK, "数据存在!", true);
}
}
System.out.println("44444444444444444444444失败"); System.out.println("44444444444444444444444失败");
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "数据异常!", false); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "数据异常!", false);
@ -400,14 +417,70 @@ public class JupyterhubController {
return new ResultEntity<>(HttpStatus.OK, success); return new ResultEntity<>(HttpStatus.OK, success);
} else { } else {
//首先重命名,然后再移动
String fileDataPath = "/etc/jupyterhub/data/"+caseName+"/" + caseName + ".ipynb"; String fileDataPath = "/etc/jupyterhub/data/"+caseName+"/" + caseName + ".ipynb";
String newDataPath = "/etc/jupyterhub/data/"+newName+"/" + newName + ".ipynb";
// 创建 File 对象 String newDataPath = "/etc/jupyterhub/data/"+caseName+"/" + newName + ".ipynb";
File oldFile = new File(fileDataPath); File oldFile = new File(fileDataPath);
File newFile = new File(newDataPath); File newFile = new File(newDataPath);
// 重命名文件或目录
System.out.println(" code output:\n" + "32423改名成功");
boolean success = oldFile.renameTo(newFile); boolean success = oldFile.renameTo(newFile);
if (success)
{
System.out.println("caseName重命名为caseName成功--");
//文件迁移
try {
String newFilePath = "/etc/jupyterhub/data/"+newName+"/";
File file1 = new File(newFilePath);
if (!file1.exists()) {
file1.mkdirs();
}
String[] command = {"mv", newDataPath, newFilePath};
// 创建一个新的进程来执行Python代码
Process process = Runtime.getRuntime().exec(command);
System.out.println("开始执行命令: " + command);
// 获取进程的输入流
BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
// 获取进程的输出流
BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// 读取Python代码的输出
String line;
StringBuilder output = new StringBuilder();
while ((line = inputStream.readLine()) != null) {
output.append(line).append("\n");
}
// 读取Python代码的错误信息
StringBuilder errors = new StringBuilder();
while ((line = errorStream.readLine()) != null) {
errors.append(line).append("\n");
}
// 等待进程执行完成
int exitCode = process.waitFor();
if (exitCode == 0) {
// 执行成功输出Python代码的结果
System.out.println("改名后的文件迁移成功!");
return new ResultEntity<>(HttpStatus.OK, "改名后的文件迁移失败了!");
} else {
// 执行失败,输出错误信息
System.err.println("改名后的文件文件迁移失败了!\n" + errors.toString());
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "更新失败!");
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
//文件移动到另一个文件夹 //文件移动到另一个文件夹
System.out.println(" code output:\n" + "改名成功"); System.out.println(" code output:\n" + "改名成功");

Loading…
Cancel
Save