import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer
{
ServerSocket webServer;
HttpServer()
{
try
{
webServer =new ServerSocket(80);
while(true)
{
Socket client = webServer.accept();
new Thread(new ClientsHandle(client)).start();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String [] args)
{
new HttpServer();
}
class ClientsHandle implements Runnable 
{
Socket client =null;
InputStream in;
PrintStream out;
private final static  String PATH="D:\\apache-tomcat-5.5.27\\webapps\\mywebsite";
ClientsHandle(Socket client)
{
this.client=client;
try
{
in=client.getInputStream();
out=new PrintStream(client.getOutputStream());
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void run()
{
String file =parse();
sendMsg(file);
}
/**
 * 解析
 * @return
 */
public String parse()
{
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String filename=null;
try
{
String content=br.readLine();
String [] contents =content.split(" ");
if(contents.length!=3)
{
sendError(400,"Cannot parse query");
return null;
}
filename=contents[1];
System.out.println("code :"+contents[0]+","+"filename"+contents[1]+","+"http version :"+contents[2]);
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return filename;
}
/**
 * 发送
 * @param filename
 */
private void sendMsg(String filename)
{
File f= new File(PATH+filename);
if(!f.exists())
{
sendError(404,"File Not Found");
return;
}
try
{
InputStream in =new FileInputStream(f);
byte [] a =new byte[(int) f.length()];
in.read(a);
out.println("HTTP/1.0 200 ok");
out.println("content-length:"+a.length);
out.println("content-type: text/html");
out.println();
out.write(a);
out.flush();
out.close();
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
 * 错误
 * @param errorCode
 * @param errorMsg
 */
private void sendError(int errorCode,String errorMsg)
{
out.println("HTTP/1.0 " + errorCode + " " + errorMsg);
out.println("content-type: text/html");
out.println();
out.println("<html>");
out.println("<title>Error Message<title>");
out.println("<body>");
out.println("<h1>ErrorCode:" + errorCode + "errorMessage:"
+ errorMsg + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
}

解决方案 »

  1.   

    编绎完全通过。debug 也是按顺序走的。就是无法将内容,错误发送到浏览器
      

  2.   

    Parse方法中的输入流不要这么早关闭。
    在调用sendMsg后,输入输出流一起关闭。
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class HttpServer
    {
        ServerSocket webServer;
        HttpServer()
        {
            try
            {
                webServer =new ServerSocket(80);
                while(true)
                {
                    Socket client = webServer.accept();
                    new Thread(new ClientsHandle(client)).start();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        public static void main(String [] args)
        {
            new HttpServer();
        }
        class ClientsHandle implements Runnable 
        {
            Socket client =null;
            InputStream in;
            PrintStream out;
            private final static  String PATH="c:\\";
            ClientsHandle(Socket client)
            {
                this.client=client;
                try
                {
                    in=client.getInputStream();
                    out=new PrintStream(client.getOutputStream());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            public void run()
            {
                String file =parse();
                sendMsg(file);
                out.close();
                try{
                 in.close();
                }catch(Exception ex){
                
                }
            }
            /**
             * 解析
             * @return
             */
            public String parse()
            {
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                String filename=null;
                try
                {
                    String content=br.readLine();
                    String [] contents =content.split(" ");
                    if(contents.length!=3)
                    {
                        sendError(400,"Cannot parse query");
                        return null;
                    }
                    filename=contents[1];
                    System.out.println("code :"+contents[0]+","+"filename"+contents[1]+","+"http version :"+contents[2]);    
                    //br.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return filename;
            }
            /**
             * 发送
             * @param filename
             */
            private void sendMsg(String filename)
            {
                File f= new File(PATH+filename);
                if(!f.exists())
                {
                    sendError(404,"File Not Found");
                    return;
                }    
                try
                {
                 System.out.println("send");
                    InputStream in =new FileInputStream(f);
                    byte [] a =new byte[(int) f.length()];
                    in.read(a);
                    out.println("HTTP/1.0 200 ok");
                    out.println("content-length:"+a.length);
                    out.println("content-type: text/html");
                    out.println();
                    out.write(a);
                    out.flush();
                    out.close();
                    in.close();
                    System.out.println("send finish");
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            /**
             * 错误
             * @param errorCode
             * @param errorMsg
             */
            private void sendError(int errorCode,String errorMsg)
            {
                out.println("HTTP/1.0 " + errorCode + " " + errorMsg);
                out.println("content-type: text/html");
                out.println();
                out.println("<html>");
                out.println("<title>Error Message<title>");
                out.println("<body>");
                out.println("<h1>ErrorCode:" + errorCode + "errorMessage:"
                        + errorMsg + "</h1>");
                out.println("</body>");
                out.println("</html>");
                out.close();
            }
        }
    }
      

  3.   

    哇哈,太感谢了。。
    就是有点不是很懂,为什么不能在parse 中关闭br................