环境WinXP 工具MyEclipse 6.5
问题描述:根据视频解说 在浏览器输入 http://localhost/liuwei.htm  会打开我存放在D:\MyEplice目录下的liuwei.html页面, 实际上提示我找不到页面,本人初学 Java 虽然顺利弄出来了 Hello World 这个问题实在不知问题出在那处,网高手指点。全部代码如下:package com.liu;import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class WebServer { public void serverStart(int port) {
try {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
new Processor(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
int port = 8045;
if (args.length == 1) {
port = Integer.parseInt(args[0]);
}
new WebServer().serverStart(port);
}
}package com.liu;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;public class Processor extends Thread {
private Socket socket;
private InputStream in;
private PrintStream out;
public final static String WEB_ROOT="D:\\MyEplice";
public Processor(Socket socket){
this.socket=socket;
try{
in=socket.getInputStream();
out=new PrintStream(socket.getOutputStream());
}catch (IOException e){
e.printStackTrace();
}
}
public void run(){
String FileName=parse(in);
SendFile(FileName);
}
public String parse(InputStream in){
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String FileName=null;
try{
String HttpMessage=br.readLine();
String[] content=HttpMessage.split(" ");
System.out.println("code"+content[0]+"filename"+content[1]+"Http version:"+content[2]);
FileName=content[1];
if(content.length!=3){
this.SendErrorMessage(400,"Client Query Error");
return null;
}
} catch(IOException e){
e.printStackTrace();
}
return FileName;
}
public void SendErrorMessage(int ErrorCode,String ErrorMessage){
out.println("HTTP/1.0"+ErrorCode+" "+ErrorMessage);
out.println("content-type: text/html");
out.println();
out.println("<html>");
out.println("<title>ErrorMessage");
out.println("</title>");
out.println("<body>");
out.println("<b1>ErrorCOde:"+ErrorCode+",Message:"+ErrorMessage+"</b1>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void SendFile(String FileName){
File file=new File(Processor.WEB_ROOT+FileName);
if (!file.exists()){
SendErrorMessage(404,"File Not Found");
return;
}
try{
InputStream in = new FileInputStream(file);
byte content[]=new byte[(int)file.length()];
in.read(content);
out.println("HTTP/1.0 200 QueryFile");
out.println("content-length:"+content.length);
out.println();
out.write(content);
out.flush();
out.close();
in.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}

解决方案 »

  1.   

    由于没有运行环境,所以没办法帮你调试,但给你几点建议:
    1.public String parse(InputStream in);是在解析HTTP请求报文头,并通过解析URL地址来解析文件名称,但是这里的解析方案太简单,可能直接将“http://localhost/liuwei.htm”作为结果返回,但实际需要的是liuwei.htm;
    2.建议将public void SendFile(String FileName)的参数FileName输出,以作为调试用,显然错误就处在FileName上。
      

  2.   

    如果是FileName 的问题话应当会有 404 File Not Found 看见,现在是直接找不到页面,我怀疑根本没有到Processor 这一段,不知道Jave怎么跟踪调试,不然我自己也可以设置断点看一下
    上面的代码我就照着视频上写的,已近核对过 。为什么他的就可以调试成功,我的就不行啊,这完全打击我学习的积极性,期待高手指点 
      

  3.   

    你的程序监听的端口是“int port = 8045”,浏览器输入的请求地址改成这个:http://localhost:8045/liuwei.htm
    不会调试,就用System.out.println(param);打印;
    建议网上搜一下如何使用;
      

  4.   

    确实如3楼的兄弟所说在浏览器中输入http://localhost:8045/liuwei.htm  就可以访问我保存在文件夹里面的文件。我有一点不明白的是一般我们上网的时候都没有输入端口号,为什么我这里就要制定端口号呢?
    其次我把WebServer类中    
         public static void main(String[] args) {
            int port =80;
            if (args.length == 1) {
                port = Integer.parseInt(args[0]);
            }
            new WebServer().serverStart(port);指定端口为80  运行程序的时候提示端口已经被占用  但是我在命令行输入 netstat -ano 没有发现那个程序占用了80端口我应该如何修改程序可以让我在浏览器栏 输入http://localhost/liuwei.htm 就可以访问我保存在文件夹里面的文件
      

  5.   

    有空看一下HTTP协议,对你理解你的程序有帮助。
    至于80端口,80是web服务的默认端口,这个看似在说服务器端使用80端口,但其实是针对客户端实施而言的。
    就拿你的程序运行结果说:没有输入端口,IE就会默认远端服务器监听的端口是80,这样它发送的HTTP报中的连接行就会设置成“localhost:80”;
    同时如果使用netstat -all|find "80",查window操作系统的端口占用可能更针对些