byte[] bytes = new byte[BUFFER_SIZE];
FileInputStream fis = null;
File file = new File(HttpServer.WEB_ROOT, request.getUri());
try {
if (file.exists()) {
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, 1024);
while (ch != -1) {
outputStream.write(bytes, 0, ch);
ch = fis.read(bytes, 0, 1024);
}
} else { // 文件未找到!
String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n"
+ "Content-Length: 23\r\n" + "\r\n" + "<h1>File Not Found</h1>";
outputStream.write(errorMessage.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null)
fis.close();
}
我的项目webroot下有index.html 我访问http://localhost:8080/index.html 代码执行了while循环里的代码,但是前台显示
是while循环里的代码写错了吗?
书上代码截图:
这里代码有问题,先检查这个吧
下面是我修改后的代码