修改文件上传io报错问题

master
whb 8 months ago
parent 68a33ce943
commit 13307631b8

@ -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);
}
}

Loading…
Cancel
Save