springboot项目打jar包后部署在linux服务器上上传文件失败

  • Post author:
  • Post category:linux


项目在开发环境中,图片可以正常上传和访问;打jar包后,文件无法上传到原来位置,路径错误

java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.9090.8099827834040313602/work/Tomcat/localhost/ROOT/file:/usr/springboot/blog-api-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/static/images/3abf4358a8fc454baae0db8de854b292.png (No such file or directory)

因为在开发环境中通过classpath可以找到相对于磁盘的绝对路径,但在打jar包后,找不到对应文件夹,所以解决办法是修改后端保存文件代码

修改后,文件正常上传,并且可以通过

url

路径正常访问,具体的

url

路径需要根据

nginx

配置以及保存路径来判断

修改前(报错)的代码:

String staticPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath();

修改后的代码:

//如果是静态方法,把getClass换成类名.class
ApplicationHome h = new ApplicationHome(getClass());
File jarF = h.getSource();
String staticPath = jarF.getParentFile().toString()



版权声明:本文为m0_49349350原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。