获取src/main/resources中的文件流
// InputStream is = 当前类名.class.getResourceAsStream("resources下文件路径");
InputStream is = UserController.class.getResourceAsStream("/file/test.txt");
获取webapp中的文件路径
//String realpath = HttpServletRequest对象.getSession().getServletContext().getRealPath("文件夹名");//获取文件夹在项目中的真实路径
String realpath = request.getSession().getServletContext().getRealPath("test");
获取resources的文件根路径
//String rootPath = 当前类名.class.getClassLoader().getResource("/").getPath();
String rootPath = Test.class.getClassLoader().getResource("/").getPath();
注意:当路径中包含中文时,使用此路径获取文件可能会报找不到文件错误,此时可以这样
String rootPath = Test.class.getClassLoader().getResource("/").toURI().getPath().substring(1);