需求要导出excel文件转成pdf在浏览器显示,文件路径:resources/templates/test.xlsx,用如下方式在本地能运行成功,但是部署到服务器报错文件找不到 File rootPath = new File(ResourceUtils.getURL("target").getPath()); //SpringBoot项目获取根目录的方式 File templatePath = new File(rootPath.getAbsolutePath(),"/classes/templates/test.xlsx"); FileInputStream fis = new FileInputStream(templatePath); Workbook workbook = new XSSFWorkbook(fis); 改成如下代码,问题解决 InputStream in = null; ClassPathResource classPathResource = new ClassPathResource("templates/test.xlsx"); in = classPathResource.getInputStream(); Workbook workbook = new XSSFWorkbook(in);