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.
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. 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.