深入剖析tomcat第一章
来源:互联网 发布:阿里云备案幕布 psd 编辑:程序博客网 时间:2024/05/16 17:24
深入剖析tomcat第一章中的代码运行有问题,静态文本发送有问题,具体代码如下:
try {
File file = new File(HttpServer.WEB_ROOT, request.getUri());
if (file.exists()) {
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);
}
}
else {
// file not found
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>";
output.write(errorMessage.getBytes());
}
}
catch (Exception e) {
// thrown if cannot instantiate a File object
System.out.println(e.toString() );
}
这个地方对于静态文件发送的时候没有只是发送了文本内容,没有HTTP协议头,所以使用浏览器是无法解析的。对于这个问题可以在发送文本文件之前加上HTTP头,方法如下:
if(file.exists()){ fis = new FileInputStream(file); String headerMessage = "HTTP/1.1 200 OK\r\n"+ "Content-Type:text/html\r\n" + "\r\n" ; outputStream.write(headerMessage.getBytes()); int ch = fis.read(bytes,0,BUFFER_SIZE); while (ch != -1){ outputStream.write(bytes,0,ch); ch = fis.read(bytes,0,BUFFER_SIZE); }}
- 深入剖析Tomcat-第一章
- 深入剖析tomcat第一章
- 深入剖析Tomcat 第一章 一个简单的Servlet服务器
- 深入剖析Tomcat
- 读《深入剖析TOMCAT》
- 《深入剖析tomcat》读书笔记1
- 《深入剖析tomcat》读书笔记2
- 深入剖析Tomcat会话机制
- 深入剖析Tomcat读书笔记一
- 深入剖析Tomcat读书笔记二
- 深入剖析tomcat 第二章
- 深入剖析Tomcat会话机制
- 深入剖析Tomcat(一)
- 深入剖析Tomcat(二)
- 深入剖析Tomcat(三)
- 深入剖析Tomcat(How Tomcat Works)
- 《深入剖析tomcat》读书笔记3--servlet容器
- 深入剖析Tomcat类加载机制
- 有关vs平台工作集
- 【转】Servlet入门总结及第一个Servlet程序
- Qt C++模版函数示例
- 快速创建一个单例singleton
- C++易错:(三)运算符优先级与结合性
- 深入剖析tomcat第一章
- 一种很帅的生成翻页列表表达式
- jbpm框架官方下载地址
- 如何杀死linux-zombie僵尸进程
- HTML小记
- JDBC模板
- jersey构建Web项目、Json格式传输
- Delphi 的RTTI机制浅探
- HDU 2612 (BFS+取最优解)