@t2652009480 8 months ago
commit 74e16b4ea9

@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.sztzjy.resource_center.annotation.AnonymousAccess; import com.sztzjy.resource_center.annotation.AnonymousAccess;
import com.sztzjy.resource_center.config.Constant; import com.sztzjy.resource_center.config.Constant;
import com.sztzjy.resource_center.util.ResultEntity; import com.sztzjy.resource_center.util.ResultEntity;
import com.sztzjy.resource_center.util.file.IFileUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
@ -24,6 +25,7 @@ import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -33,6 +35,8 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
@ -46,7 +50,8 @@ import java.util.Optional;
@RestController @RestController
public class JupyterhubController { public class JupyterhubController {
@Autowired
IFileUtil iFileUtil;
@ApiOperation("创建容器") @ApiOperation("创建容器")
@GetMapping("/jumpJupyerHub") @GetMapping("/jumpJupyerHub")
@ -236,6 +241,50 @@ public class JupyterhubController {
} }
@ApiOperation("ipynb文件重命名")
@GetMapping("/uploadIpynbByName")
@AnonymousAccess
public ResultEntity uploadIpynbByName(@ApiParam("参数为:案例名") String caseName,String newName) throws IOException {
// 对参数进行 URL 编码
String encodedCaseName = URLEncoder.encode(caseName, StandardCharsets.UTF_8.toString());
String encodedNewName = URLEncoder.encode(newName, StandardCharsets.UTF_8.toString());
String url = "https://jrdsj.sztzjy.com:598/api/jupyterhub/uploadFileByIpynbName?caseName=" + encodedCaseName+ "&newName="+encodedNewName ;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(url);
// 执行 GET 请求
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity);
// 解析 JSON 响应
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonResponse = objectMapper.readTree(responseString);
// 假设返回的 JSON 对象包含 "data" 字段
JsonNode dataNode = jsonResponse.get("data");
//html也需要重命名
String htmlOldIpynb = "/usr/local/tianzeProject/jupyter/md/html/" + caseName + ".html";
String htmlNewIpynb = "/usr/local/tianzeProject/jupyter/md/html/" + newName + ".html";
// 创建 File 对象
File oldFile = new File(htmlOldIpynb);
File newFile = new File(htmlNewIpynb);
// 重命名文件或目录
boolean success = oldFile.renameTo(newFile);
return new ResultEntity<>(HttpStatus.OK,dataNode != null && dataNode.asBoolean());
}
}
}

@ -381,18 +381,22 @@ public class StuJupyterController {
//文件查询 ,判断有无文件 //文件查询 ,判断有无文件
@ApiOperation("Html文件上传") @ApiOperation("html文件上传")
@PostMapping("/importHtml") @PostMapping("/importHtml")
@AnonymousAccess @AnonymousAccess
public ResultEntity importHtml(@ApiParam("案例名") String caseName,@RequestPart MultipartFile file) { public ResultEntity importHtml(@ApiParam("案例名") String caseName,@RequestPart MultipartFile file) {
String originalFilename = file.getOriginalFilename();
int i = originalFilename.lastIndexOf("."); String replace = file.getOriginalFilename().replace(".ipynb", ".html");
String substring = originalFilename.substring(i); int i = replace.lastIndexOf(".");
String substring = replace.substring(i+1);
if ("html".equals(substring)) { if ("html".equals(substring)) {
String htmlPath = "/usr/local/tianzeProject/jupyter/md/html"; String htmlPath = "/usr/local/tianzeProject/jupyter/md/html";
//String htmlPath = "D:\\home\\";
System.out.println("文件上传:"+substring);
String upload = localFileUtil.uploadByRelativePath(file, htmlPath,caseName); String upload = localFileUtil.uploadByRelativePath(file, htmlPath,caseName);
if (upload != null) { if (upload != null) {

@ -178,21 +178,23 @@ public class LocalFileUtil implements IFileUtil{
public String uploadByRelativePath(MultipartFile file, String relativePath,String caseName) { public String uploadByRelativePath(MultipartFile file, String relativePath,String caseName) {
Assert.isTrue(!file.isEmpty(), "文件不存在"); Assert.isTrue(!file.isEmpty(), "文件不存在");
System.out.println("文件不存在!");
try { try {
// 创建目录如果不存在 // 创建目录如果不存在
File directory = new File(relativePath); File directory = new File(relativePath);
if (!directory.exists()) { if (!directory.exists()) {
directory.mkdirs(); directory.mkdirs();
} }
System.out.println("1");
// 设置目标文件路径 // 设置目标文件路径
String result = caseName == null ? file.getOriginalFilename() : caseName; String result = caseName == null ? file.getOriginalFilename() : caseName;
System.out.println("2");
int i = file.getOriginalFilename().lastIndexOf("."); int i = file.getOriginalFilename().lastIndexOf(".");
System.out.println("3");
String substring = file.getOriginalFilename().substring(i); String substring = file.getOriginalFilename().substring(i);
System.out.println("4");
File destFile = new File(relativePath + File.separator + result+substring); File destFile = new File(relativePath + File.separator + result+substring);
System.out.println("5");
file.transferTo(destFile); file.transferTo(destFile);
return "上传成功!"; return "上传成功!";
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {

Loading…
Cancel
Save