spring mvc 读取文件

//Resource resource = applicationContext.getResource("classpath:META-INF/print/templete.txt");
//InputStream is = resource.getInputStream();
//String path1 = this.getClass().getClassLoader().getResource("/").getPath();
String path =Thread.currentThread().getContextClassLoader().getResource("/META-INF/print/templete.txt").getPath();
//System.out.println(path1);
//System.out.println(path2);
File file = new File(path);
String result = "";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
//BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
result = result + "\n" + s;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}


model.addAttribute("templete", result);

你可能感兴趣的:(java)