-
Notifications
You must be signed in to change notification settings - Fork 135
/
UrlValidate.java
91 lines (82 loc) · 2.89 KB
/
UrlValidate.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by JohnTsai on 16/1/27.
*/
public class UrlValidate {
private String fileName;
public UrlValidate(String fileName){
this.fileName = fileName;
}
public String readFile(String fileName){
File file = new File(fileName);
Path path = file.toPath();
String str = null;
try {
str = new String(Files.readAllBytes(path));
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public List<String> getStringByRegex(String str, String patternStr){
List<String> list = new ArrayList<>();
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(str);
while (matcher.find()){
list.add(matcher.group());
}
return list;
}
public Boolean checkValidate(String urlStr){
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3*1000);
connection.setReadTimeout(30*1000);
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
connection.connect();
int code = connection.getResponseCode();
System.out.println("responseCode:--->"+code);
return code>=200&&code<400;
} catch (IOException e) {
System.out.println("fail...");
e.printStackTrace();
return false;
}
}
public void checkValidate(){
System.out.println("-----start checking------");
String readme = readFile(this.fileName);
List<String> urlList = getStringByRegex(readme,"\\(http.*?\\)");
List<String> failList = new ArrayList<>();
for(String string:urlList){
String replace = string.replace("(", "").replace(")","");
System.out.println("checking..."+replace);
if(checkValidate(replace)==false)
failList.add(replace);
}
if(failList.size()==0)
System.out.println("All sites are available");
else{
System.out.println("--------------------\nSites not available:");
for(String site:failList){
System.out.println(site);
}
}
}
public static void main(String[] args){
UrlValidate urlValidate = new UrlValidate("./README.md");
urlValidate.checkValidate();
}
}