-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerUtils.java
executable file
·72 lines (55 loc) · 1.65 KB
/
ServerUtils.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
/**
* ServerUtils Class
*
*/
import java.nio.file.*;
import java.io.*;
import java.util.*;
import java.text.*;
public class ServerUtils {
private final static String HTTP_DATE_FORMAT = "EEE, dd MMM yyyy hh:mm:ss zzz";
/**
* Returns the current date of the system
*
* @return String The current date as a string following HTTP format
*
*/
public static String getCurrentDate() {
return dateLongToString(System.currentTimeMillis());
}
/**
* Returns the content type of the file object
*
* @param object The File object to be probed for its type
* @return String Type of the object
*
*/
public static String getContentType(File object) throws IOException {
return Files.probeContentType(object.toPath());
}
/**
* Returns the content length of the file object as a string
*
* @param object The File object to be probed for its content length
* @return String Length of the object
*
*/
public static String getContentLength(File object) {
return String.format("%d", object.length());
}
/**
* Returns the last modified date of the object
*
* @param object The File object to be probed for its last modified date
* @return String Last modified date of the object
*
*/
public static String getLastModified(File object) {
return dateLongToString(object.lastModified());
}
// Coverts a date from long (in milli seconds) format to a string format
private static String dateLongToString(long longDate) {
SimpleDateFormat simple = new SimpleDateFormat(HTTP_DATE_FORMAT);
return simple.format(new Date(longDate));
}
}