有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何修复java。伊奥。FileNotFoundException:url的响应:“404:未找到”http://localhost:7001/Socketwar/Servlet'

我使用下面的代码从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) 个答案

  1. # 1 楼答案

    这个答案是对聊天的回应

    以下是我的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();
    
    }