编写代码实现根据用户在浏览器URL中请求的资源返回对应的结果数据 HTTP请求的组成如下:
(含图)
-------------------------------------------------------------------------------------
public class HttpHomework {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final String CRLF = "\r\n";
try {
ServerSocket server = new ServerSocket(8080);
lExecutor executor = new ScheduledThreadPoolExecutor(30);
while (true) {
final Socket con = serveraccept();
Runnable serverThread = new Runnable() {
public void __【1】____{
try {
HttPBufferedInputStream blis = new HttPBufferedInputStream(con____【2】____);//按行读取数据的工具
byte[] buf = new byte[1024];
int c = blisreadLine(buf, 0, buf___【3】__);
String res = new String(buf, 0, c);
String[] elements = ressplit(" ");
res = elements[__【4】___]substring(___【5】___);
File f = new File(res);
FileInputStream fis = new FileInputStream(f);
long length = f__【6】____;
OutputStream os = con_____【7】___;
String responseHeader = "HTTP/11 200 OK" + CRLF;
if (resendsWith("htm") || resendsWith("html")) {
responseHeader += "Content-Type: text/html"
+ CRLF;
} else {
responseHeader += "_______【8】________: application/stream"
+ CRLF;
}
responseHeader += "Content-Length: " + length
+ CRLF;
responseHeader +=CRLF;
oswrite(responseHeadergetBytes());
while ((c = fisread(buf, 0, buflength)) !=___【9】____) {
oswrite(buf, 0, c);
}
fisclose();
osclose();
} catch (Exception ex) {
exprintStackTrace();
}
}
};
executorexecute(serverThread);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}