自动转换html文件

master
whb 8 months ago
parent 5c044ddd4f
commit aa815fe510

@ -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/
* <p>
*
*/
@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<MultipartFile> dataFile,@ApiParam("案例名")String caseName) {
public ResultEntity uploadFile(@ApiParam("文件") List<MultipartFile> 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);
}

Loading…
Cancel
Save