简体   繁体   English

HTTP端口80上的URLConnection FileNotFoundException

[英]URLConnection FileNotFoundException on HTTP port 80

I have a JSP file named aniltest.jsp which has following code: 我有一个名为aniltest.jsp的JSP文件,其中包含以下代码:

<%
    try {
        URL aURL = new URL("http://localhost:80/admin/anil1.txt");

        BufferedReader in = new BufferedReader(new InputStreamReader(aURL.openStream()));

        String inputLine;

        while ((inputLine = in.readLine()) != null)
            System.out.println("content of anil1.txt: " + inputLine);

        in.close();
    } catch (IOException e) {
        System.out.println("Error reading content of url");
        e.printStackTrace();
    } 
%>

In the above code I am trying to read a text file named anil1.txt which is located at http://localhost:80/admin/anil1.txt 在上面的代码中,我试图读取一个名为anil1.txt的文本文件,该文件位于http:// localhost:80 / admin / anil1.txt

I get below error when I run: http://localhost:80/aniltest.jsp 运行时出现以下错误: http:// localhost:80 / aniltest.jsp

java.io.FileNotFoundException: http://localhost:80/admin/anil1.txt
  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
  at _jsp._aniltest__jsp._jspService(_aniltest__jsp.java:93)
  at _jsp._aniltest__jsp._jspService(_aniltest__jsp.java:31)
  at com.caucho.jsp.JavaPage.service(JavaPage.java:64)
  at com.caucho.jsp.Page.pageservice(Page.java:548)
  at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:194)
  at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:156)
  at com.caucho.server.webapp.AccessLogFilterChain.doFilter(AccessLogFilterChain.java:95)
  at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:289)
  at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:838)
  at com.caucho.network.listen.TcpSocketLink.dispatchRequest(TcpSocketLink.java:1349)
  at com.caucho.network.listen.TcpSocketLink.handleRequest(TcpSocketLink.java:1305)
  at com.caucho.network.listen.TcpSocketLink.handleRequestsImpl(TcpSocketLink.java:1289)
  at com.caucho.network.listen.TcpSocketLink.handleRequests(TcpSocketLink.java:1197)
  at com.caucho.network.listen.TcpSocketLink.handleAcceptTaskImpl(TcpSocketLink.java:993)
  at com.caucho.network.listen.ConnectionTask.runThread(ConnectionTask.java:117)
  at com.caucho.network.listen.ConnectionTask.run(ConnectionTask.java:93)
  at com.caucho.network.listen.SocketLinkThreadLauncher.handleTasks(SocketLinkThreadLauncher.java:169)
  at com.caucho.network.listen.TcpSocketAcceptThread.run(TcpSocketAcceptThread.java:61)
  at com.caucho.env.thread2.ResinThread2.runTasks(ResinThread2.java:173)
  at com.caucho.env.thread2.ResinThread2.run(ResinThread2.java:118)

Though I can access the file http://localhost:80/admin/anil1.txt in the web browser without any problem: I am using latest Resin server. 尽管我可以在Web浏览器中访问文件http://localhost:80/admin/anil1.txt ,但没有任何问题:我正在使用最新的Resin服务器。 I am totally stuck and can't figure it out what is the problem. 我完全被困住了,无法找出问题所在。

Try this 尝试这个

URLConnection  uCon = aUrl.openConnection();
InputStream is = uCon.getInputStream();

then read bytes from the inputstream 然后从输入流读取字节

The code does not seem to have any problem. 该代码似乎没有任何问题。 You can check if localhost resolves to anywhere else, you can try 127.0.0.1 instead. 您可以检查localhost是否解析为其他任何地址,而可以尝试使用127.0.0.1。 Mentioning port is not mandatory, it should work otherwise too (80 is default). 提及端口不是强制性的,否则端口也应起作用(默认为80)。

Try sniffing the request and response with wireshark if the problem continues. 如果问题仍然存在,请尝试使用wireshark嗅探请求和响应。

Is the url protected by any authentication? 网址是否受任何身份验证保护? Try clearing browser cookie and cache and initiate a fresh request. 尝试清除浏览器Cookie和缓存并启动新请求。

Hey guys File not found exception was because of Skype which was using port 80 for incoming connections. 嘿,文件未找到异常是由于Skype使用端口80进行传入连接。 After chaning the port it works well. 更改端口后,它可以正常工作。 Thanks for your help. 谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM