在Jsp中将绝对路径转换为相对路径

时间:2014-12-26 21:34:48

标签: java jsp path

这是保存文本文件的绝对路径。现在我想转换成相对路径。

<a class="navbar-brand" href="index.jsp"> <%
String jspPath = "C:\\Users\\Farshid\\workspace\\STPT\\WebContent\\file\\title.txt"; //this is my absolute path.
BufferedReader reader = new BufferedReader(new FileReader(jspPath));
StringBuilder sb = new StringBuilder();
String line;
line = reader.readLine();%> <%=line%></a>

现在我如何在jsp中改变相对路径?这是我的项目目录图片。 enter image description here

1 个答案:

答案 0 :(得分:-1)

尝试:

String path = getServletConfig().getServletContext().getRealPath("/file/title.txt");
BufferedReader reader = new BufferedReader(new FileReader(path));