修复bug

This commit is contained in:
menxipeng
2025-09-24 20:20:53 +08:00
parent 0b75730d37
commit e6c413d907
12 changed files with 891 additions and 2 deletions

View File

@@ -0,0 +1,66 @@
package com.ruoyi.web.controller.client;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* 客户端文件控制器
*
* @author ruoyi
*/
@RestController
@RequestMapping("/client/file")
public class ClientFileController extends BaseController {
/**
* 读取私有目录文件
*
* @param fileName 文件名
* @return 文件内容
*/
@GetMapping("/readPrivateFile/{fileName}")
public String readPrivateFile(@PathVariable("fileName") String fileName) {
try {
// 参数校验
if (fileName == null || fileName.trim().isEmpty()) {
return "错误:文件名不能为空";
}
// 构建文件路径
String filePath = "/home/private/" + fileName.trim();
File file = new File(filePath);
// 检查文件是否存在
if (!file.exists()) {
return "错误:文件不存在";
}
// 检查是否为文件(不是目录)
if (!file.isFile()) {
return "错误:指定路径不是文件";
}
// 安全检查:确保文件在指定目录内,防止路径遍历攻击
String canonicalPath = file.getCanonicalPath();
if (!canonicalPath.startsWith("/home/private/")) {
return "访问被拒绝";
}
// 读取文件内容并直接返回
return FileUtils.readFileToString(file, StandardCharsets.UTF_8);
} catch (IOException e) {
logger.error("读取文件失败: " + e.getMessage(), e);
return "错误:读取文件失败 - " + e.getMessage();
} catch (Exception e) {
logger.error("系统错误: " + e.getMessage(), e);
return "错误:系统错误";
}
}
}