-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThread.java
More file actions
38 lines (35 loc) · 908 Bytes
/
MyThread.java
File metadata and controls
38 lines (35 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MyThread extends Thread {
private Socket socket;
public MyThread(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
InputStream inputStream=null;
OutputStream outputStream=null;
try {
inputStream=socket.getInputStream();
outputStream=socket.getOutputStream();
String request=Util.getStringFromInputStream(inputStream);
String url=Util.getUrlFromRequestHead(request);
System.out.println("请求的文件:"+url.substring(1));
//url为/或/index.html
Response response=new Response(outputStream, url);
response.send();
} catch (Exception e) {
e.printStackTrace();
}finally {
//处理完成,关闭流
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}