From b109048649a1a3d67175a618d394f83237ce128b Mon Sep 17 00:00:00 2001 From: whb <17803890193@163.com> Date: Tue, 6 Aug 2024 09:59:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=87=8D=E5=A4=8D=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=8D=A0=E7=94=A8=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/stu/JupyterhubController.java | 95 ++++++++++++++++--- 1 file changed, 84 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/sztzjy/financial_bigdata/controller/stu/JupyterhubController.java b/src/main/java/com/sztzjy/financial_bigdata/controller/stu/JupyterhubController.java index 1ebc1cc..65d7097 100644 --- a/src/main/java/com/sztzjy/financial_bigdata/controller/stu/JupyterhubController.java +++ b/src/main/java/com/sztzjy/financial_bigdata/controller/stu/JupyterhubController.java @@ -76,11 +76,17 @@ public class JupyterhubController { @GetMapping("/fileExistState") @AnonymousAccess public ResultEntity fileExistState(@ApiParam("案例名") String caseName) { + + if (caseName == null) + { + return null; + } + //存储文件路径 String path = "/etc/jupyterhub/data/"; - //etc/jupyterhub/data/xxx/ + // /etc/jupyterhub/data/caseName/ String newCreatFile = path + caseName+"/"; @@ -92,9 +98,11 @@ public class JupyterhubController { } //判断有无ipynb文件,进行迁移 // 使用File类表示这个文件 + ///etc/jupyterhub/data/caseName.ipynb String fileResource = path + caseName + ".ipynb"; + File myFile = new File(fileResource); System.out.println("222222222222"+myFile); @@ -158,27 +166,36 @@ public class JupyterhubController { //判断文件是否存在与文件夹里 再文件夹里就返回,不存在就返回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"; + System.out.println("3333333"+pathIpynb); File file1 = new File(pathIpynb); if (!file1.exists()) { //判断数据集是否存在 - + System.out.println("判断数据集是否存在123456789==="+pathIpynb); String sonPath = newCreatFile + "/data/"; if (new File(sonPath).exists()) { return new ResultEntity<>(HttpStatus.OK, "数据存在!", true); }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失败"); return new ResultEntity<>(HttpStatus.BAD_REQUEST, "数据异常!", false); @@ -400,14 +417,70 @@ public class JupyterhubController { return new ResultEntity<>(HttpStatus.OK, success); } else { + //首先重命名,然后再移动 + 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 newFile = new File(newDataPath); - // 重命名文件或目录 - System.out.println(" code output:\n" + "32423改名成功"); 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" + "改名成功");