258 lines
7.3 KiB
Markdown
258 lines
7.3 KiB
Markdown
# ImportService 硬编码路径修复总结
|
||
|
||
## 修复概述
|
||
成功修复了 `ImportService.java` 中除表名外的所有文件路径硬编码问题,提升了系统的可移植性和可维护性。
|
||
|
||
## 修复内容
|
||
|
||
### 1. 配置文件修改
|
||
|
||
#### application-dev.yml (开发环境)
|
||
```yaml
|
||
# 新增网络上传文件路径配置
|
||
net:
|
||
upload:
|
||
win:
|
||
filePath: "D:\\fileAll\\"
|
||
targetPath: "D:\\testFile"
|
||
linux:
|
||
filePath: "/home/fileAll/"
|
||
targetPath: "/home/testFile"
|
||
upload-two:
|
||
win:
|
||
filePath: "D:\\fileAllTwo\\"
|
||
targetPath: "D:\\testFileTwo"
|
||
linux:
|
||
filePath: "/home/fileAllTwo/"
|
||
targetPath: "/home/testFileTwo"
|
||
jzt:
|
||
win:
|
||
filePath: "D:\\fileAll\\"
|
||
targetPath: "D:\\testFile"
|
||
linux:
|
||
filePath: "/opt/fileAll/"
|
||
targetPath: "/opt/testFile"
|
||
```
|
||
|
||
#### application-prod.yml (生产环境)
|
||
```yaml
|
||
# 新增网络上传文件路径配置(Docker环境安全路径)
|
||
net:
|
||
upload:
|
||
win:
|
||
filePath: ${NET_UPLOAD_WIN_FILEPATH:"D:\\fileAll\\"}
|
||
targetPath: ${NET_UPLOAD_WIN_TARGETPATH:"D:\\testFile"}
|
||
linux:
|
||
filePath: ${NET_UPLOAD_LINUX_FILEPATH:"/app/data/fileAll/"}
|
||
targetPath: ${NET_UPLOAD_LINUX_TARGETPATH:"/app/data/testFile"}
|
||
upload-two:
|
||
win:
|
||
filePath: ${NET_UPLOAD_TWO_WIN_FILEPATH:"D:\\fileAllTwo\\"}
|
||
targetPath: ${NET_UPLOAD_TWO_WIN_TARGETPATH:"D:\\testFileTwo"}
|
||
linux:
|
||
filePath: ${NET_UPLOAD_TWO_LINUX_FILEPATH:"/app/data/fileAllTwo/"}
|
||
targetPath: ${NET_UPLOAD_TWO_LINUX_TARGETPATH:"/app/data/testFileTwo"}
|
||
jzt:
|
||
win:
|
||
filePath: ${NET_JZT_WIN_FILEPATH:"D:\\fileAll\\"}
|
||
targetPath: ${NET_JZT_WIN_TARGETPATH:"D:\\testFile"}
|
||
linux:
|
||
filePath: ${NET_JZT_LINUX_FILEPATH:"/app/data/fileAll/"}
|
||
targetPath: ${NET_JZT_LINUX_TARGETPATH:"/app/data/testFile"}
|
||
```
|
||
|
||
### 2. Java代码修改
|
||
|
||
#### 2.1 新增配置属性
|
||
在 `ImportService.java` 中添加了12个配置属性:
|
||
```java
|
||
// 网络上传文件路径配置
|
||
@Value("${net.upload.win.filePath}")
|
||
private String netUploadWinFilePath;
|
||
|
||
@Value("${net.upload.win.targetPath}")
|
||
private String netUploadWinTargetPath;
|
||
|
||
@Value("${net.upload.linux.filePath}")
|
||
private String netUploadLinuxFilePath;
|
||
|
||
@Value("${net.upload.linux.targetPath}")
|
||
private String netUploadLinuxTargetPath;
|
||
|
||
@Value("${net.upload-two.win.filePath}")
|
||
private String netUploadTwoWinFilePath;
|
||
|
||
@Value("${net.upload-two.win.targetPath}")
|
||
private String netUploadTwoWinTargetPath;
|
||
|
||
@Value("${net.upload-two.linux.filePath}")
|
||
private String netUploadTwoLinuxFilePath;
|
||
|
||
@Value("${net.upload-two.linux.targetPath}")
|
||
private String netUploadTwoLinuxTargetPath;
|
||
|
||
@Value("${net.jzt.win.filePath}")
|
||
private String netJztWinFilePath;
|
||
|
||
@Value("${net.jzt.win.targetPath}")
|
||
private String netJztWinTargetPath;
|
||
|
||
@Value("${net.jzt.linux.filePath}")
|
||
private String netJztLinuxFilePath;
|
||
|
||
@Value("${net.jzt.linux.targetPath}")
|
||
private String netJztLinuxTargetPath;
|
||
```
|
||
|
||
#### 2.2 新增路径配置辅助方法
|
||
```java
|
||
/**
|
||
* 根据操作系统类型获取网络上传文件路径配置
|
||
* @param type 路径类型: upload, upload-two, jzt
|
||
* @return 包含filePath和targetPath的Map
|
||
*/
|
||
private Map<String, String> getNetUploadPathConfig(String type) {
|
||
Map<String, String> pathConfig = new HashMap<>();
|
||
|
||
if ("upload".equals(type)) {
|
||
if (system.equals("win")) {
|
||
pathConfig.put("filePath", netUploadWinFilePath);
|
||
pathConfig.put("targetPath", netUploadWinTargetPath);
|
||
} else {
|
||
pathConfig.put("filePath", netUploadLinuxFilePath);
|
||
pathConfig.put("targetPath", netUploadLinuxTargetPath);
|
||
}
|
||
} else if ("upload-two".equals(type)) {
|
||
if (system.equals("win")) {
|
||
pathConfig.put("filePath", netUploadTwoWinFilePath);
|
||
pathConfig.put("targetPath", netUploadTwoWinTargetPath);
|
||
} else {
|
||
pathConfig.put("filePath", netUploadTwoLinuxFilePath);
|
||
pathConfig.put("targetPath", netUploadTwoLinuxTargetPath);
|
||
}
|
||
} else if ("jzt".equals(type)) {
|
||
if (system.equals("win")) {
|
||
pathConfig.put("filePath", netJztWinFilePath);
|
||
pathConfig.put("targetPath", netJztWinTargetPath);
|
||
} else {
|
||
pathConfig.put("filePath", netJztLinuxFilePath);
|
||
pathConfig.put("targetPath", netJztLinuxTargetPath);
|
||
}
|
||
}
|
||
|
||
return pathConfig;
|
||
}
|
||
```
|
||
|
||
#### 2.3 修改的方法
|
||
|
||
**hookUpNet 方法** (行1611):
|
||
```java
|
||
// 修改前
|
||
if(system.equals("win")){
|
||
filePath = "D:\\fileAll\\";
|
||
targetPath = "D:\\testFile";
|
||
FileUtil2.makedir(filePath);
|
||
}else {
|
||
filePath = "/home/fileAll/";
|
||
targetPath = "/home/testFile";
|
||
FileUtil2.makedir(filePath);
|
||
}
|
||
|
||
// 修改后
|
||
Map<String, String> pathConfig = getNetUploadPathConfig("upload");
|
||
String filePath = pathConfig.get("filePath");
|
||
String targetPath = pathConfig.get("targetPath");
|
||
FileUtil2.makedir(filePath);
|
||
```
|
||
|
||
**hookUpTwoNet 方法** (行1650) - 用户特别关注:
|
||
```java
|
||
// 修改前
|
||
if(system.equals("win")){
|
||
filePath = "D:\\fileAllTwo\\";
|
||
targetPath = "D:\\testFileTwo";
|
||
FileUtil2.makedir(filePath);
|
||
}else {
|
||
filePath = "/home/fileAllTwo/";
|
||
targetPath = "/home/testFileTwo";
|
||
FileUtil2.makedir(filePath);
|
||
}
|
||
|
||
// 修改后
|
||
Map<String, String> pathConfig = getNetUploadPathConfig("upload-two");
|
||
String filePath = pathConfig.get("filePath");
|
||
String targetPath = pathConfig.get("targetPath");
|
||
FileUtil2.makedir(filePath);
|
||
```
|
||
|
||
**hookUpJztNet 方法** (行2742):
|
||
```java
|
||
// 修改前
|
||
if(system.equals("win")){
|
||
filePath = "D:\\fileAll\\";
|
||
targetPath = "D:\\testFile";
|
||
FileUtil2.makedir(filePath);
|
||
}else {
|
||
filePath = "/opt/fileAll/";
|
||
targetPath = "/opt/testFile";
|
||
FileUtil2.makedir(filePath);
|
||
}
|
||
|
||
// 修改后
|
||
Map<String, String> pathConfig = getNetUploadPathConfig("jzt");
|
||
String filePath = pathConfig.get("filePath");
|
||
String targetPath = pathConfig.get("targetPath");
|
||
FileUtil2.makedir(filePath);
|
||
```
|
||
|
||
### 3. 保留的硬编码
|
||
|
||
按照用户要求,保留了以下硬编码:
|
||
- `hookUpXiaoGan` 方法中的硬编码表名: `String tableName = "wsdajh_table_20220402164528";`
|
||
|
||
## 修复效果
|
||
|
||
### 1. 环境兼容性
|
||
- ✅ 支持跨平台部署 (Windows/Linux)
|
||
- ✅ 支持Docker容器环境
|
||
- ✅ 支持不同环境的配置差异
|
||
|
||
### 2. 可维护性
|
||
- ✅ 路径配置统一管理
|
||
- ✅ 易于修改和扩展
|
||
- ✅ 遵循12-Factor App原则
|
||
|
||
### 3. 安全性
|
||
- ✅ 生产环境使用安全的Docker路径 `/app/data/`
|
||
- ✅ 支持环境变量覆盖
|
||
- ✅ 避免硬编码敏感路径
|
||
|
||
### 4. 向后兼容
|
||
- ✅ 开发环境保持原有路径
|
||
- ✅ 不影响现有业务逻辑
|
||
- ✅ API接口保持不变
|
||
|
||
## 使用说明
|
||
|
||
### 开发环境
|
||
直接使用默认配置路径,或在 `application-dev.yml` 中修改。
|
||
|
||
### 生产环境
|
||
可以通过环境变量覆盖配置:
|
||
```bash
|
||
export NET_UPLOAD_LINUX_FILEPATH="/custom/path/fileAll/"
|
||
export NET_UPLOAD_LINUX_TARGETPATH="/custom/path/testFile"
|
||
```
|
||
|
||
### Docker环境
|
||
在Docker Compose或Kubernetes配置中设置环境变量。
|
||
|
||
## 验证结果
|
||
- ✅ 编译通过:`mvn compile -q`
|
||
- ✅ 无语法错误
|
||
- ✅ 无硬编码路径残留
|
||
- ✅ 保持原有功能完整性
|
||
|
||
## 总结
|
||
成功将 `ImportService.java` 中的3个网络文件上传方法的硬编码路径全部配置化,提升了系统的可移植性、可维护性和部署灵活性。同时保持了原有的业务逻辑不变,确保了系统的稳定性。 |