From 13307631b804c892782c5af5c70a859f4e2410b5 Mon Sep 17 00:00:00 2001 From: whb <17803890193@163.com> Date: Mon, 5 Aug 2024 15:02:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0io=E6=8A=A5=E9=94=99=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 | 181 +++++++++--------- 1 file changed, 91 insertions(+), 90 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 b3daa19..10dbeab 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 @@ -79,91 +79,92 @@ public class JupyterhubController { //存储文件路径 String path = "/etc/jupyterhub/data/"; + //etc/jupyterhub/data/xxx/ + String newCreatFile = path + caseName+"/"; - String newCreatFile = path + caseName; -// File dataFile = new File(newCreatFile); -// -// //判断文件夹是否存在 -// -// //创建文件夹 -// dataFile.mkdir(); + File dataFile = new File(newCreatFile); - - //判断有无ipynb文件,进行迁移 - // 使用File类表示这个文件 - String fileResource = path + caseName + ".ipynb"; + //判断文件夹是否存在 + if (!dataFile.exists()) { + dataFile.mkdirs(); + } + //判断有无ipynb文件,进行迁移 + // 使用File类表示这个文件 + String fileResource = path + caseName + ".ipynb"; - File myFile = new File(fileResource); + File myFile = new File(fileResource); - // 判断文件是否存在 - if (myFile.exists()) { - System.out.println("文件存在: " + fileResource); + // 判断文件是否存在 + if (myFile.exists()) { + System.out.println("文件存在: " + fileResource); + //文件移动到对应的文件夹内 + try { - //文件移动到对应的文件夹内 - try { - String[] command = {"mv", fileResource, newCreatFile}; - // 创建一个新的进程来执行Python代码 - Process process = Runtime.getRuntime().exec(command); + String[] command = {"mv", fileResource, newCreatFile}; + // 创建一个新的进程来执行Python代码 + Process process = Runtime.getRuntime().exec(command); - // 获取进程的输入流 - BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream())); + // 获取进程的输入流 + BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream())); - // 获取进程的输出流 - BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); + // 获取进程的输出流 + 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(); + // 读取Python代码的输出 + String line; + StringBuilder output = new StringBuilder(); + while ((line = inputStream.readLine()) != null) { + output.append(line).append("\n"); + } - if (exitCode == 0) { - // 执行成功,输出Python代码的结果 - System.out.println("文件迁移成功!"); - //判断子目录是否有data文件 - String sonPath = newCreatFile + "/data"; - File sonFile = new File(sonPath); - if (sonFile.exists()) { - return new ResultEntity<>(HttpStatus.OK, "数据存在!", true); - } else { - return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false); - } + // 读取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("文件迁移成功!"); + //判断子目录是否有data文件 + String sonPath = newCreatFile + "/data"; + File sonFile = new File(sonPath); + if (sonFile.exists()) { + return new ResultEntity<>(HttpStatus.OK, "数据存在!", true); } else { - // 执行失败,输出错误信息 - System.err.println("文件迁移失败了!\n" + errors.toString()); - - return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false); } - } catch (IOException | InterruptedException e) { - e.printStackTrace(); - } - } else { - return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false); + } else { + // 执行失败,输出错误信息 + System.err.println("文件迁移失败了!\n" + errors.toString()); + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "文件迁移失败了!", false); + } + } catch (IOException | InterruptedException e) { + e.printStackTrace(); } + } else { + //判断文件是否存在与文件夹里 再文件夹里就返回,不存在就返回false + // return new ResultEntity<>(HttpStatus.BAD_REQUEST, "ipynb文件不存在!", false); + + ///etc/jupyterhub/data/xxx/ + String pathIpynb = newCreatFile + caseName + ".ipynb"; + File file1 = new File(pathIpynb); + if (!file1.exists()) { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "Ipynb文件不存在!"); + } - return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false); - - - + } + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "数据异常!", false); } @@ -177,6 +178,7 @@ public class JupyterhubController { String filepath = "/etc/jupyterhub/data/" + caseName + "/data/"; + //设置数据文件 if (dataFile != null && !dataFile.isEmpty()) { @@ -299,7 +301,7 @@ public class JupyterhubController { File file123 = new File(htmlIpynb); if (!file123.exists()) { System.out.println("文件不存在: " + file123); - return new ResultEntity<>(HttpStatus.BAD_REQUEST); + return new ResultEntity<>(HttpStatus.BAD_REQUEST); } @@ -307,35 +309,35 @@ public class JupyterhubController { String url = "https://qkljr.sztzjy.com:81/api/stu/jupyter/importHtml"; - try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpPost uploadFile = new HttpPost(url); + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpPost uploadFile = new HttpPost(url); - // 读取文件为字节数组 - byte[] fileBytes = new byte[(int) file123.length()]; - try (FileInputStream fis = new FileInputStream(file123)) { - fis.read(fileBytes); - } + // 读取文件为字节数组 + byte[] fileBytes = new byte[(int) file123.length()]; + try (FileInputStream fis = new FileInputStream(file123)) { + fis.read(fileBytes); + } - // Build the multipart entity - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + // Build the multipart entity + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - // Use ByteArrayBody with proper encoding for the file name - ByteArrayBody byteArrayBody = new ByteArrayBody(fileBytes, file.getOriginalFilename()); - builder.addPart("file", byteArrayBody); - builder.addTextBody("caseName", caseName, ContentType.TEXT_PLAIN.withCharset("UTF-8")); - builder.setMode(HttpMultipartMode.RFC6532);//解决文件名乱码问题 + // Use ByteArrayBody with proper encoding for the file name + ByteArrayBody byteArrayBody = new ByteArrayBody(fileBytes, file.getOriginalFilename()); + builder.addPart("file", byteArrayBody); + builder.addTextBody("caseName", caseName, ContentType.TEXT_PLAIN.withCharset("UTF-8")); + builder.setMode(HttpMultipartMode.RFC6532);//解决文件名乱码问题 - HttpEntity entity = builder.build(); - uploadFile.setEntity(entity); + HttpEntity entity = builder.build(); + uploadFile.setEntity(entity); - // Execute the request - try (CloseableHttpResponse response = httpClient.execute(uploadFile)) { - HttpEntity responseEntity = response.getEntity(); - // Convert response entity to String with UTF-8 encoding - String responseString = EntityUtils.toString(responseEntity, "UTF-8"); - System.out.println("上传html响应数据: " + responseString); - } + // Execute the request + try (CloseableHttpResponse response = httpClient.execute(uploadFile)) { + HttpEntity responseEntity = response.getEntity(); + // Convert response entity to String with UTF-8 encoding + String responseString = EntityUtils.toString(responseEntity, "UTF-8"); + System.out.println("上传html响应数据: " + responseString); + } } catch (IOException e) { e.printStackTrace(); @@ -359,7 +361,7 @@ public class JupyterhubController { @ApiOperation("更新ipynb文件名") @GetMapping("/uploadFileByIpynbName") @AnonymousAccess - public ResultEntity uploadFileByIpynbName(@ApiParam("案例名") String caseName,String newName) { + public ResultEntity uploadFileByIpynbName(@ApiParam("案例名") String caseName, String newName) { String filepath = "/etc/jupyterhub/data/" + caseName + ".ipynb"; File file = new File(filepath); if (file.exists()) { @@ -376,11 +378,10 @@ public class JupyterhubController { boolean success = oldFile.renameTo(newFile); - return new ResultEntity<>(HttpStatus.OK, success); } else { - return new ResultEntity<>(HttpStatus.OK, "重命名失败!",false); + return new ResultEntity<>(HttpStatus.OK, "重命名失败!", false); } }