Java HTTP服务器无法正常工作

Hel*_*ele 7 java http

我在家里主持一个网页.我使用Java创建了自己的HTTP服务器.这是一个SSCCE:

if(command.startsWith("GET"))
{
            //client is a socket on which I reply.
            PrintWriter pw = new PrintWriter(client.getOutputStream(), true);
    String commule = command.split(" ");
    if(commule[0].equals("GET"))
    {
        if(commule[1].contains("."))
        {
            File file = new File(GEQO_SERVER_ROOT + commule[1].substring(1).replaceAll("%20", " "));
            if(file.exists())
            {
                OutputStream out = client.getOutputStream();
                InputStream stream = new FileInputStream(file);

                String response = new String();
                response += "HTTP/1.1 200 OK\r\n";
                response += "Date: Thu, 08 Aug 2013 08:49:37 GMT\r\n";
                response += "Content-Type: text/html\r\n";
                response += "Content-Length: " + file.length() + "\r\n";
                response += "Connection: keep-alive\r\n";
                response += "\r\n";
                pw.write(response); //Assume I already initialized pw as a PrintWriter
                                    pw.flush();
                copy(stream, out);
                stream.close();
                out.close();
            }
            else
            {
                pw.write("<html><h1>The request 404ed.</h1>");
                pw.write("<body>The requested URL <b>" + commule[1] + "</b> could not be found on this server.</body></html>");
                                    pw.flush();
            }
        }
        else
        {
            BufferedReader br = new BufferedReader(new FileReader(GEQO_SERVER_ROOT + commule[1].substring(1) + "main.html"));
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) 
            {
                pw.print(sCurrentLine);
            }
            br.close();
        }
    }
    else
    {
        pw.println("Unrecognized HTTP command.");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是main.html来源:

<html>
<title>Geqo Server</title>
<body>Geqo server online and functioning!</body>
</html>

问题是,当我尝试使用Chrome访问此页面时,它会正确显示(至少在使用127.0.0.1时).但是当我尝试在127.0.0.1上的Firefox上访问它时,它可以工作,但只是给了我html源代码.IE也只给我源.任何人都可以告诉我为什么Firefox和IE只显示源,而不是解析它?

我认为这包含一些线索(Firebug截图):

Firebug截图

我的消息来源似乎是一个<pre>标签.我不知道为什么,但不是那种问题吗?

我端口转发.这是页面人员:( http://110.172.170.83:17416/抱歉,Stackoverflow不允许数字链接.)

编辑:我发现了问题.但在我解释之前,感谢Bart为SSCCE,我曾经将其与我的代码进行比较.这就是问题:if第八行的语句if(commule[1].contains("."))导致代码跳过这里的大部分代码.在那个相应的else块中,甚至没有命令发送头.感谢artbristol指出这一点.

提前致谢.

art*_*tol 5

你的printwriter没有刷新(正如Ernest指出的那样),所以没有发送HTTP头.查看直接连接的结果 - 它只返回原始数据,没有标题.

nc 110.172.170.83 17416
GET /

<html><title>Geqo Server</title><body>Geqo server online and functioning!</body></html>
Run Code Online (Sandbox Code Playgroud)

编写HTTP服务器很难.除非这是练习,否则您应该使用轻量级现有的,例如Jetty,或JDK中内置的Sun HTTP服务器.

编辑 - PrintWriter真的不适合做HTTP.它旨在处理逐行数据,例如写入磁盘的文件.它还依赖于文本编码和行结尾的特定于平台的设置.检查HTTP规范以获取有关正确的HTTP服务器应如何工作的更多详细信息.