当前位置:编程学习 > JAVA >>

webserver的小程序运行错误,求教

文件路径和名字都没有错,问题估计是出在读取输入流的问题上,但是初学,弄了很久,找不到具体错在哪,请大家帮忙看下
2个源代码:

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 = "C:\\Eclipse\\workspace";
public Processor(Socket socket) {
this.socket = socket;
try {
in = socket.getInputStream();
System.out.println(in);
out = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String filename;
try {
filename = parse(in);
sendFile(filename);
} catch (IOException e) {
e.printStackTrace();
}
}
public String parse(InputStream in) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String httpMessage = br.readLine();
if (httpMessage == null || httpMessage.length() == 0) {
sendErrorMessage(400, "Client invoke error");
return null;
}
String[] content = httpMessage.split("");
if (content.length != 3) {
sendErrorMessage(400, "Client query error");
return null;
}
String filename = content[1];
System.out.println("code:" + content[0] + ",filenme" + content[1]
+ ",http version:" + content[2]);
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("<head><title>Error " + errorCode + "--" + errorMessage
+ "</title></head>");
out.println("<h1>" + errorCode + " " + errorMessage + "</h1>");
out.println("</html>");
out.println();
out.flush();
out.close();
}
public void sendFile(String filename) {
File file = new File(Processor.WEB_ROOT + filename);
System.out.println(file);
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();
}
}
}

另一个主程序:

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 = 80;
if (args.length == 1) {
port = Integer.parseInt(args[0]);
}
new WebServer().serverStart(port);
}
}


错误报告:java.io.FileNotFoundException: C:\Eclipse\workspacenull (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at com.rongqiang.Processor.sendFile(Processor.java:94)
at com.rongqiang.Processor.run(Processor.java:37)
我就想弄明白读取输入流的错误在哪(如果是读取问题的话)? --------------------编程问答-------------------- C:\Eclipse\workspacenull

多明显啊,这个字符串分2部分
C:\Eclipse\workspace 和 null

前面的你自己保证了目录存在,可后面的文件名呢?为啥是null ?

你自己搞定! --------------------编程问答-------------------- 调试的时候,不要用IE,用FireFox吧,能看到你的程序给浏览器的真正反馈。

大版主要LZ自己搞定的,我也不好意思多说什么,是细节问题。只要修改一个字符就行了。
对了,这一处排除了后还有一处。
祝顺利。 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 正在学习中
--------------------编程问答--------------------  String[] content = httpMessage.split("");
split(" ");
少个空格。
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,