resource.getFile()
expects the resource itself to be available on the file system, i.e. it can’t be nested inside a jar file. This is why it works when you run your application in STS but doesn’t work once you’ve built your application and run it from the executable jar. Rather than usinggetFile()
to access the resource’s contents, I’d recommend usinggetInputStream()
instead. That’ll allow you to read the resource’s content regardless of where it’s located.
大意是ResourceUtils的getFile方法无法获取jar中打包进去的资源文件,所以建议采用getInputStream方法获取resources
目录下的资源文件。
// 读取excel模板
ClassPathResource classPathResource = new ClassPathResource(templatePath);
InputStream fileInputStream = classPathResource.getInputStream();
String fileName = "导入模板" + System.currentTimeMillis() + ".xlsx";