0

I have a JSP file named aniltest.jsp which has following code:

<%
    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

I get below error when I run: 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. I am totally stuck and can't figure it out what is the problem.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Dreamer
  • 35
  • 1
  • 7

3 Answers3

0

Try this

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

then read bytes from the inputstream

Zaid Qureshi
  • 1,203
  • 1
  • 10
  • 15
0

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. Mentioning port is not mandatory, it should work otherwise too (80 is default).

Try sniffing the request and response with wireshark if the problem continues.

Is the url protected by any authentication? Try clearing browser cookie and cache and initiate a fresh request.

Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45
  • No URL is not protected by any authentication. When I try 127.0.0.1 it doesn't work. Tried clearing browser and fresh request still not working. – Dreamer Apr 15 '15 at 17:43
  • Can you try writing the same code on a Java class instead of JSP and try if it works? What you're experiencing is unusual and hence we are trying weird ways. – Pavan Kumar Apr 16 '15 at 04:19
  • It works well from a Java class code instead of JSP. What is the problem why it does't work with JSP? – Dreamer Apr 16 '15 at 09:11
0

Hey guys File not found exception was because of Skype which was using port 80 for incoming connections. After chaning the port it works well. Thanks for your help.

Dreamer
  • 35
  • 1
  • 7
  • That's interesting. From my previous comment - What you're experiencing is unusual and hence we are trying weird ways. Am curious about how it worked when you tried from browser. Can you try from browser keeping the Skype running? – Pavan Kumar Apr 16 '15 at 12:57
  • When my Skype is running I can't access it as I said because Skype was using port 80. When I change the port of Skype it works well. – Dreamer Apr 17 '15 at 14:12