JSP - 文件名,目录名或卷标语法不正确

时间:2014-07-08 18:03:31

标签: java jsp servlets path

希望有人可以帮助我。我在尝试显示JSP页面时遇到此错误:

18:41:22,674 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.io.FileNotFoundException: http:\localhost:8080\leopardcreek\xxxx.pem 
(The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)

我知道路径是正确的,文件就在那里。该URL中的语法不正确?

我的Java部分:

URL myURL = new URL("http://localhost:8080/leopardcreek/xxxx.pem");
req.setAttribute("keyUrl", myURL);

JSP:

<%
String keyUrl = request.getAttribute("keyUrl").toString(); 
InputStream inStream = new FileInputStream(keyUrl); 
%>

1 个答案:

答案 0 :(得分:0)

您需要将其读作URLStream

String keyUrlString = request.getAttribute("keyUrl").toString();
URL keyURL   = new URL(keyUrlString);
BufferedReader in = new BufferedReader(
new InputStreamReader(keyURL.openStream()));

String inputLine;
  while ((inputLine = in.readLine()) != null)
     //process

注意:完全不鼓励将此代码放在视图模板(.jsp)上