-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsumer.java
More file actions
84 lines (63 loc) · 2.12 KB
/
Consumer.java
File metadata and controls
84 lines (63 loc) · 2.12 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package webcrawler;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.tools.FileObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Consumer {
PrintStream myconsole;
public void Start() {
crawl();
}
private void crawl() {
String crawlUrl = this.getCrawlUrl();
System.out.println(crawlUrl);
HashSet<String> anchors = new HashSet<String>();
try {
Document doc = Jsoup.connect(crawlUrl).get();
Elements hrefs = doc.select("a");
for (Element e : hrefs) {
String anchor = e.attr("href").trim();
anchors.add(anchor);
System.out.println(anchor);
}
} catch (IOException ex) {
Logger.getLogger(WebCrawler.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("-------------");
try {
FileOutputStream fos = new FileOutputStream(new File("F:\\tesst.txt"));
myconsole = new PrintStream(fos);
System.setOut(myconsole);
} catch (FileNotFoundException ex) {
Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
}
for (String s : anchors) {
System.out.println(s);
}
try{
if ((new File("F:\\tesst.txt")).exists()) {
Process p = Runtime
.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler F:\\tesst.txt");
p.waitFor();
} else {
System.out.println("File does not exist");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private String getCrawlUrl() {
String url = CrawlerGui.url;
return url;
}
}