问题:
springboot vue项目中上传图片或文件直接上传到项目根目录下的upload目录下,并没有直接上传到target/classes/static/upload中;
过程排查:
在application.yml中进行了正常的静态资源配置,配置了spring.resources.static-locations为classpath:static/,file:static/;
检查FileController代码,
File path = new File(ResourceUtils.getURL("classpath:static").getPath());
if (!path.exists()) {
path = new File(""); // 回退到当前工作目录(项目根目录)
}
File upload = new File(path.getAbsolutePath(), "/upload/");
其中路径处理部分:
File path = new File(ResourceUtils.getURL("classpath:static").getPath());
如果这个路径不存在,就使用当前目录(new File(""))。然后创建upload目录:upload = new File(path.getAbsolutePath(), "/upload/")。说明上传路径的配置存在问题,可能是路径解析失败;
问题解决:
检查项目目录路径,发现目录中存在空格,删去空格,能正常上传,目录不能纯英文或者有空格