//props.load(new FileInputStream(ClassUtils.getDefaultClassLoader().getResource("").getPath() + File.separator + "email.properties")); props.load(EmailService.class.getClassLoader().getResourceAsStream("email.properties"));
2、生成Excel
//获取classes路径 //String classPath = ClassUtils.getDefaultClassLoader().getResource("").getPath(); //File tempDirectory = new File(classPath); //生成tempExcel文件夹到classes的父文件夹中,直接在classes文件中生成会导致项目重启出错 //String tempPath = tempDirectory.getParentFile().getParentFile().getParent() + File.separator + "tempExcel"; //String fileName = tempPath + File.separator+timeCode + "人人花统计日报.xls"; //获取当前项目所在目录 String tempPath = System.getProperty("user.dir") + File.separator + "tempExcel"; String fileName = tempPath + File.separator+timeCode + "人人花统计日报.xls";
说明:注释的方法本地eclipse测试正常,项目打成jar包到服务器后,路径错误,会在前面添加file:,导致文件读取失败。