问题
spring boot项目上传到Linux服务器后,头像上传后报404。但是我的打包的jar包在idea客户端运行是没问题的
解决
- 错误代码
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//获取jar包所在目录
ApplicationHome h = new ApplicationHome(getClass());
File jarF = h.getSource();
//在jar包所在目录下生成一个upload文件夹用来存储上传的图片
String dirPath = jarF.getParentFile().toString()+"\\static\\upload\\";
registry.addResourceHandler("/upload/**").addResourceLocations("file:"+dirPath);
}
- 修改
将String dirPath = jarF.getParentFile().toString()+"\\static\\upload\\";修改为String dirPath = jarF.getParentFile().toString()+"/static/upload/";
问题解决
博客讲述了在SpringBoot项目中,将应用部署到Linux服务器后,头像上传功能出现404错误的问题。作者发现错误在于路径分隔符使用不当,原本使用的是Windows风格的反斜杠,修正为Linux兼容的正斜杠后问题得到解决。此问题涉及到文件资源处理和不同操作系统间的路径差异。

4966

被折叠的 条评论
为什么被折叠?



