获取项目内文件路径的方式
1.bean类获取
1.先获取HttpServletRequest
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
2.通过HttpServletRequest 获取路径
String filePath = request.getSession().getServletContext().getRealPath("/static/claim.xlsx");
注:getRealPath("/static/claim.xlsx");中写你webapp下文件路径
2.普通类工具类获取
1.先获取基本路径
String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", “”);
2.获取classpath 后在通过字符串拼接得到最终路径
注1:路径中的斜杠是正斜杠即问号斜杠“/”,根据实际情况可replace成反斜杠“\”。
注2:工具类方法更多时候含有static,此时this不可用,可使用new yourclass()当前类的方式代替。