-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecieveThread.java
77 lines (62 loc) · 2.7 KB
/
RecieveThread.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package socket;
import com.sun.xml.internal.ws.util.ByteArrayBuffer;
import java.io.*;
import java.net.Socket;
import java.net.URLDecoder;
public class RecieveThread extends Thread {
private Socket s;
public RecieveThread(Socket s) {
this.s = s;
}
@Override
public void run() {
InputStream is = null;
try {
is = s.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
String s1 = reader.readLine();
// System.out.println(s1);
// System.out.println(s1.indexOf('/'));
String resource = s1.substring(s1.indexOf('/'), s1.lastIndexOf('/') - 5);
System.out.println("the resource you request is: " + resource);
String resData = null;
StringBuffer stringBuffer = new StringBuffer();
File htmlData = new File("res/html" + resource);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(htmlData);
} catch (FileNotFoundException e) {
e.printStackTrace();
StringBuilder response = new StringBuilder();
response.append("HTTP/1.1 200 OK\r\n")
.append("Content-Length: ").append(resData.getBytes().length).append("\r\n")
.append("Content-Type: text/html; charset=utf-8\r\n")
.append("\r\n")
.append("404 not found").append("\r\n");
System.out.println(response);
OutputStream outputStream = this.s.getOutputStream();
outputStream.write(response.toString().getBytes());
outputStream.flush();
}
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read(bytes)) != -1) {
stringBuffer.append(new String(bytes, 0, len));
}
resData = stringBuffer.toString();
//System.out.println(resData);
StringBuilder response = new StringBuilder();
response.append("HTTP/1.1 200 OK\r\n")
.append("Content-Length: ").append(resData.getBytes().length).append("\r\n")
.append("Content-Type: text/html; charset=utf-8\r\n")
.append("\r\n")
.append(resData).append("\r\n");
System.out.println(response);
OutputStream outputStream = this.s.getOutputStream();
outputStream.write(response.toString().getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}