Repository navigation
Expand file tree
/
Copy pathServer.java
More file actions
67 lines (65 loc) · 2.37 KB
/
Copy pathServer.java
File metadata and controls
67 lines (65 loc) · 2.37 KB
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
package Internet;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Server {
private final int port;
private final String directory;
public Server(int p, String d)
{
this.port=p;
this.directory=d;
}
public void ServerStart()
{
try
{
ServerSocket server=new ServerSocket(port);
while (!ServerDisable.need){
Socket client = server.accept();
PrintStream responseStream = new PrintStream(client.getOutputStream());
Scanner requestStream = new Scanner(client.getInputStream());
if (requestStream.hasNextLine()) {
String URL = RequestedFile(requestStream);
File f = new File(directory + URL);
if (f.exists()) {
int SuccessCode = 200;
String SuccessMessage = "OK";
this.sendResponse(responseStream, SuccessCode, SuccessMessage, "text/html", f.length());
Scanner FileScan = new Scanner(f);
while (FileScan.hasNextLine()) {
String s = FileScan.nextLine();
responseStream.println(s);
}
} else {
String nf = "Not Found";
int l = 8;
this.sendResponse(responseStream, 404, nf, "text/plain", l);
responseStream.println("Not Found");
}
client.close();
responseStream.flush();
}
}
} catch (UnknownHostException e) {
System.out.println("Host not found");
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendResponse(PrintStream w, int statusCode, String statusText, String type, long size)
{
w.printf("HTTP/1.1 %s %s%n", statusCode, statusText);
w.printf("Content-Type: %s%n", type);
w.printf("Content-Length: %s%n%n", size);
}
private String RequestedFile(Scanner a)
{
String request = a.nextLine();
return request.split(" ")[1];
}
}