谷歌浏览器访问 http://localhost:8080/index.html 出现ERR_INVALID_HTTP_RESPONSE错误
原因: 没有添加响应返回头信息
正确代码:
if (file.exists()) {
String header = "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"\r\n";
output.write(header.getBytes());
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1) {
output.write(bytes, 0, ch);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
}