在Windows中是支持\和/两种分隔符的,当然默认是\作为路径分隔符 。
在UNIX中则只支持/作为路径分隔符
所以在编写程序的时候这一点有必要注意一下(解决方案如下):
a.编码的时候使用/作为分隔符
b.使用File.sperator来作为分隔符,这个方法会返回一个字符串,代表当前文件系统的路径分隔符,可以将该值保存为一个变量,用该变量作为路径分隔符来拼接路径(该方法较为麻烦,但比上边更稳妥)
c.JDK7引入了一个新的文件接口PATH以及一个静态的文件创建类PATHS,创建文件的时候把路径中的目录名填入方法,它会自动根据当前文件系统的路径分隔符去拼接一个完整的文件路径。
转载自:JAVA中对于UNIX和Windows系文件分隔符的差异
File.sperator是与系统有关的默认名称分隔符。此字段被初始化为包含系统属性 file.separator 值的第一个字符。在 UNIX 系统上,此字段的值为 '/';在 Microsoft Windows 系统上,它为 '\'。
可以如文章java代码怎么实现在linux系统上上传文件到windows服务器上
所说,
所以我的代码修改如下:
1.读取打包jar后的resources下的文件
public K8sManager(@Value("${spring.profiles.active}")String profile) {
String kubeConfigPath = profile +"-"+"config";
try {
//TODO 打包后找不到根目录下的文件,所以将k8s的config文件转移到了classpath下新建的k8sconfig目录下
//这个读不到 可能去掉"classpath:"有可能读到,试过欢迎留言 File configFile = ResourceUtils.getFile("classpath:"+k8sConfig+kubeConfigPath);
// ClassPathResource类的构造方法接收路径名称,自动去classpath路径下找文件
ClassPathResource classPathResource = new ClassPathResource(k8sConfig+kubeConfigPath);
InputStream in = classPathResource.getInputStream();
// 读取每一行config配置加入\n存入到StringBuilder中
BufferedReader bufReader=new BufferedReader(new InputStreamReader(in));
StringBuilder configYAML = new StringBuilder();
String line;
while((line=bufReader.readLine())!=null) {
configYAML.append(line);
configYAML.append("\n");
}
final Config config = Config.fromKubeconfig(String.valueOf(configYAML));
this.client = new DefaultKubernetesClient(config);
} catch (IOException e) {
log.error("init k8s client error:",e);
e.printStackTrace();
}
}
ClassPathResource classPathResource = new ClassPathResource("resources下的配置文件路径如 “k8sconfig”+File.separator+kubeConfigPath ");
InputStream in = classPathResource.getInputStream();
通过流的方式读取打包为jar包后的resources下的文件
2.读写项目运行环境下的文件,并使用freemarker替换模板数据
@Autowired
FreeMarkerConfigurer freeMarkerConfigurer;
//k8s yaml文件路径
public static final String k8sYamlPath = File.separator+"tmp";
//设置模板使用参数
Map<String, Object> model = new HashMap<String, Object>();
model.put("deployns", deployns);
model.put("deployapp", deployapp);
model.put("deployproject", deployproject);
model.put("deployuri", deployuri);
model.put("deployprofile", deployprofile);
model.put("updatetime", updateTime);
model.put("gitdeployer", gitdeployer);
model.put("deployregistry", deployregistry);
model.put("imagens", imagens);
model.put("deploytag", deploytag);
model.put("registryprefix", registryprefix);
model.put("deployreplicas",deployreplicas);
//加载模板
Template t = null;
try {
t = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
//todo 将文件传到项目运行环境的/tmp下
File upFile = new File(k8sYamlPath);
if(!upFile.exists()) {
upFile.mkdir();
}
File file = new File(upFile, k8sYamlName);
//替换参数
t.process(model, new FileWriter(file));
//todo 使用yaml资源清单创建或替换资源
//TODO 从tmp下读取替换后的文件
File yaml = ResourceUtils.getFile(upFile + k8sYamlName);
InputStream in = new FileInputStream(yaml);
List<HasMetadata> orReplace = client.load(in)
.inNamespace(deployns)
.createOrReplace();
log.info("创建资源清单资源中....:{}",orReplace.stream().count());
spring:
#配置freemarker
freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置文档类型
content-type: text/html
# 设置页面编码格式
charset: UTF-8
# 设置页面缓存
cache: false
prefer-file-system-access: false
# 设置ftl文件路径(!!)
template-loader-path:
- classpath:/templates
settings:
classic_compatible: true