From aa815fe510b9fce08dfb14415fbac5e3a40c2a4c Mon Sep 17 00:00:00 2001 From: whb <17803890193@163.com> Date: Fri, 2 Aug 2024 17:15:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=BD=AC=E6=8D=A2html?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/stu/JupyterhubController.java | 329 +++++++++++++++--- 1 file changed, 273 insertions(+), 56 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 f98c67b..08f64f3 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 @@ -8,15 +8,23 @@ import com.sztzjy.financial_bigdata.util.file.IFileUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.ByteArrayBody; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.util.List; /** @@ -36,19 +44,19 @@ public class JupyterhubController { @ApiOperation("创建容器") @GetMapping("/jumpJupyerHub") @AnonymousAccess - public ResultEntity jumpJupyerHub(@ApiParam("案例名")String caseName) { + public ResultEntity jumpJupyerHub(@ApiParam("案例名") String caseName) { String username = IdUtil.fastSimpleUUID(); - String substring = username.substring(2,9); - String name = "zy" +substring; + String substring = username.substring(2, 9); + String name = "zy" + substring; try { JupyterHubTokenManager.createUser(name); System.out.println("创建用户成功!"); - JupyterHubTokenManager.startServer(name,caseName); + JupyterHubTokenManager.startServer(name, caseName); System.out.println("启动服务!"); String url = JupyterHubTokenManager.generateToken(name); - return new ResultEntity<>(HttpStatus.OK,"容器创建成功!",url); + return new ResultEntity<>(HttpStatus.OK, "容器创建成功!", url); } catch (Exception e) { throw new RuntimeException(e); @@ -58,20 +66,20 @@ public class JupyterhubController { /** - * - * 1.进入页面首先发送请求判断数据是否存在 - * 2.不存在直接调用上传接口,将数据传输过来 格式 文件夹-(ipynb,data/数据集) - * - * 教师端上传案例直接上传到对应文件夹下, 教师端管理时候 - * - */ + * 1.进入页面首先发送请求判断数据是否存在 + * 2.不存在直接调用上传接口,将数据传输过来 格式 文件夹-(ipynb,data/数据集) + *

+ * 教师端上传案例直接上传到对应文件夹下, 教师端管理时候 + */ @ApiOperation("判断数据集是否存在") @GetMapping("/fileExistState") @AnonymousAccess - public ResultEntity fileExistState(@ApiParam("案例名")String caseName) { + public ResultEntity fileExistState(@ApiParam("案例名") String caseName) { //存储文件路径 String path = "/etc/jupyterhub/data/"; + + String newCreatFile = path + caseName; File dataFile = new File(newCreatFile); @@ -81,10 +89,54 @@ public class JupyterhubController { //创建文件夹 dataFile.mkdir(); + //修改权限 + try { + // 创建 chmod 命令以递归方式修改权限 + String[] command = {"chmod", "-R", "777", newCreatFile}; + // 创建一个新的进程来执行Python代码 + Process process = Runtime.getRuntime().exec(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("权限修改成功!"); + + } else { + // 执行失败,输出错误信息 + System.err.println("权限修改失败了!\n" + errors.toString()); + + } + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + //判断有无ipynb文件,进行迁移 // 使用File类表示这个文件 - String fileResource = path+caseName+".ipynb"; + String fileResource = path + caseName + ".ipynb"; File myFile = new File(fileResource); @@ -126,33 +178,33 @@ public class JupyterhubController { if (exitCode == 0) { // 执行成功,输出Python代码的结果 System.out.println("文件迁移成功!"); - return new ResultEntity<>(HttpStatus.OK,"数据不存在!",false); + return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false); } else { // 执行失败,输出错误信息 - System.err.println("文件迁移失败了!\n"+ errors.toString()); - return new ResultEntity<>(HttpStatus.OK,"数据不存在!",false); + 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); + return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false); } - return new ResultEntity<>(HttpStatus.OK,"数据不存在!",false); + return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false); - }else { + } else { - //判断子目录是否有data文件 + //判断子目录是否有data文件 - String sonPath = newCreatFile +"/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); + if (sonFile.exists()) { + return new ResultEntity<>(HttpStatus.OK, "数据存在!", true); + } else { + return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false); } } @@ -162,10 +214,10 @@ public class JupyterhubController { @ApiOperation("数据集不存在文件上传") @PostMapping("/uploadFile") @AnonymousAccess - public ResultEntity uploadFile(@ApiParam("文件")List dataFile,@ApiParam("案例名")String caseName) { + public ResultEntity uploadFile(@ApiParam("文件") List dataFile, @ApiParam("案例名") String caseName) { - String filepath = "/etc/jupyterhub/data/"+caseName+"/data/"; + String filepath = "/etc/jupyterhub/data/" + caseName + "/data/"; //设置数据文件 @@ -173,53 +225,219 @@ public class JupyterhubController { for (MultipartFile file : dataFile) { - iFileUtil.uploadResource(file,filepath,null); + iFileUtil.uploadResource(file, filepath, null); + + try { + // 创建 chmod 命令以递归方式修改权限 + String[] command = {"chmod", "-R", "777", filepath}; + // 创建一个新的进程来执行Python代码 + Process process = Runtime.getRuntime().exec(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("权限修改成功!"); + + } else { + // 执行失败,输出错误信息 + System.err.println("权限修改失败了!\n" + errors.toString()); + + } + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + } } - return new ResultEntity<>(HttpStatus.OK,"上传成功"); + return new ResultEntity<>(HttpStatus.OK, "上传成功"); } /** - * 上传ipynb文件 创建文件夹 /文件名 到金融大数据服务器上 - * 上传html 上传到 资源中心服务器 html - * 创建案例 跳转 (文件名创建 不存在 ) - */ - + * 上传ipynb文件 创建文件夹 /文件名 到金融大数据服务器上 + * 上传html 上传到 资源中心服务器 html + * 创建案例 跳转 (文件名创建 不存在 ) + */ @ApiOperation("超管端或者教师端上传ipynb文件") @PostMapping("/uploadFileByIpynb") @AnonymousAccess - public ResultEntity uploadFileByIpynb(@RequestPart @ApiParam("文件")MultipartFile file, @ApiParam("案例名")String caseName) { + public ResultEntity uploadFileByIpynb(@RequestPart @ApiParam("文件") MultipartFile file, @ApiParam("案例名") String caseName) { + + + String filepath = "/etc/jupyterhub/data/" + caseName; + iFileUtil.uploadResource(file, filepath, caseName); + + //上传之后转换html到前端展示 + + String newIpynb = "/etc/jupyterhub/data/" + caseName + ".ipynb"; + String htmlIpynb = "/etc/jupyterhub/html/" + caseName + ".html"; + + try { + String[] command = {"jupyter", "nbconvert", "--to", "html", newIpynb, "--output", htmlIpynb}; + + // 创建一个新的进程来执行Python代码 + Process process = Runtime.getRuntime().exec(command); + + // 获取进程的输入流 + BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream())); + + // 获取进程的输出流 + BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); + +// // 向进程的输入流写入Python代码 +// process.getOutputStream().write(pythonCode.getBytes()); +// process.getOutputStream().flush(); +// process.getOutputStream().close(); + + // 读取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("Python code output:\n" + "HTML转换成功"); + + File file123 = new File(htmlIpynb); + if (!file123.exists()) { + System.out.println("文件不存在: " + file123); + return new ResultEntity<>(HttpStatus.BAD_REQUEST); + } - String filepath = "/etc/jupyterhub/data/"+caseName; + //调用数据上传到外汇服务器 - iFileUtil.uploadResource(file,filepath,caseName); + String url = "https://qkljr.sztzjy.com:81/api/stu/jupyter/importHtml"; + 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); + } - return new ResultEntity<>(HttpStatus.OK,"上传成功"); + + // 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);//解决文件名乱码问题 + + 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); + } + } catch (IOException e) { + e.printStackTrace(); + + } + return new ResultEntity(HttpStatus.OK, output.toString()); + } else { + // 执行失败,输出错误信息 + System.out.println("Python code output:\n" + "HTML转换失败!!!!!"); + return new ResultEntity(HttpStatus.OK, errors.toString()); + } + + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + return new ResultEntity<>(HttpStatus.OK, "上传成功"); } + @ApiOperation("更新ipynb文件名") + @GetMapping("/uploadFileByIpynbName") + @AnonymousAccess + public ResultEntity uploadFileByIpynbName(@ApiParam("案例名") String caseName,String newName) { + String filepath = "/etc/jupyterhub/data/" + caseName + ".ipynb"; + File file = new File(filepath); + if (file.exists()) { + //重命名 + + // 新文件或目录路径 + String newPath = "/etc/jupyterhub/data/" + newName + ".ipynb"; // 请将此路径替换为实际路径 + + // 创建 File 对象 + File oldFile = new File(filepath); + File newFile = new File(newPath); + + // 重命名文件或目录 + boolean success = oldFile.renameTo(newFile); + + + + return new ResultEntity<>(HttpStatus.OK, success); + } else { + + return new ResultEntity<>(HttpStatus.OK, "重命名失败!",false); + } + + } + //判断ipynb文件是否存在 @ApiOperation("判断是否上传ipynb文件") @GetMapping("/uploadFileByIpynbExist") @AnonymousAccess - public ResultEntity uploadFileByIpynbExist(@ApiParam("案例名")String caseName) { - String filepath = "/etc/jupyterhub/data/"+caseName+".ipynb"; + public ResultEntity uploadFileByIpynbExist(@ApiParam("案例名") String caseName) { + String filepath = "/etc/jupyterhub/data/" + caseName + ".ipynb"; File file = new File(filepath); - if (file.exists()) - { - return new ResultEntity<>(HttpStatus.OK,true); - }else { - return new ResultEntity<>(HttpStatus.OK,false); + if (file.exists()) { + return new ResultEntity<>(HttpStatus.OK, true); + } else { + return new ResultEntity<>(HttpStatus.OK, false); } } @@ -227,15 +445,14 @@ public class JupyterhubController { @ApiOperation("删除ipynb文件") @GetMapping("/delUploadFileByIpynb") @AnonymousAccess - public ResultEntity delUploadFileByIpynb(@ApiParam("案例名")String caseName) { - String filepath = "/etc/jupyterhub/data/"+caseName+".ipynb"; + public ResultEntity delUploadFileByIpynb(@ApiParam("案例名") String caseName) { + String filepath = "/etc/jupyterhub/data/" + caseName + ".ipynb"; File file = new File(filepath); - if (file.exists()) - { + if (file.exists()) { file.delete(); - return new ResultEntity<>(HttpStatus.OK,true); + return new ResultEntity<>(HttpStatus.OK, true); } - return new ResultEntity<>(HttpStatus.OK,true); + return new ResultEntity<>(HttpStatus.OK, true); }