使用java -jar部署的应用 和之前的文件上传略有所不同
所以获取不到路径
旧的方式
#本地环境下可以成功 k8s环境 无法获取因为是jar形式部署的
Objects.requireNonNull(this.getClass().getClassLoader().getResource("")).getPath();
修改后的上传方式如下
//获取根目录
File path;
try {
path = new File(ResourceUtils.getURL("classpath:").getPath());
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
if (!path.exists()) {
path = new File("");
}
//若是上传目录为/static/excel/upload/,则能够以下获取:
File upload = new File(path.getAbsolutePath(), "static/excel/upload/");
if (!upload.exists()) {
boolean mkdirs = upload.mkdirs();
log.info("目录不存在创建:{}", mkdirs ? "成功" : "失败");
}
//上传路径
String actualPath = upload.getPath();
log.info("actualPath: {}",actualPath)



