如何修复java.io.FileNotFoundException:响应:' 404:Not Found' for url:' http:// localhost:7001 / Socket-war / Servlet'

时间:2014-05-06 05:00:13

标签: java url filenotfoundexception inputstreamreader buffered

我使用下面的代码从我的Java类调用servlet:

  URL url = new URL( "http://localhost:7001/Socket-war/Servlet" );
  new BufferedReader(new InputStreamReader(url.openStream()));  

获得例外:

java.io.FileNotFoundException: Response: '404: Not Found' for url: 'http://localhost:7001/Socket-war/Servlet'  

一切都很好;我以前也在其他程序中使用过它;但现在提出例外......

有人告诉我为什么???任何建议或建议都会非常值得注意。

谢谢!!!

1 个答案:

答案 0 :(得分:0)

这个答案是为了回应发生的聊天。

以下是我的servlet代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          response.getWriter().println("Hello World");
            response.getWriter().flush();
            response.getWriter().close();
    }

以下是我的连接代码,它可以正常工作。

public static void main(String[] args) throws IOException {
    System.setProperty("http.proxyHost", "proxy.***.com");
    System.setProperty("http.proxyPort", "####");
     Authenticator authenticator = new Authenticator() {
                    public PasswordAuthentication getPasswordAuthentication() {
                        return (new PasswordAuthentication("user",
                                "pwd".toCharArray()));
                    }
                };
    Authenticator.setDefault(authenticator);
     URL url = new URL( "http://localhost:8080/ImageCanvas/Servlet2" );
     BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
     String line;
     while((line=br.readLine())!= null){
         System.out.println(line);
         System.out.flush();
     }
     br.close();

}