Webseiten herunterladen und speichern
Aus Byte-Welt Wiki
Version vom 10. Juni 2019, 07:56 Uhr von L-ectron-X (Diskussion | Beiträge)
Folgender, sehr einfacher, vollständiger Code zeigt, wie man Webseiten mit Java downloaden und auf der Festplatte speichern kann:
1 import java.io.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.net.*;
5 import java.util.*;
6 import javax.swing.*;
7 import javax.swing.filechooser.*;
8
9 /**
10 * GrapHtmlDemo zeigt, wie man mit Java Quelltext von Webseiten herunterladen und speichern kann.
11 *
12 * @author Gernot Segieth
13 */
14 public class GrapHtmlDemo {
15 private final JFrame frame;
16 private JTextField targetField;
17 private JTextArea tArea;
18 private JTabbedPane tabbedPane;
19 private JFileChooser chooser;
20
21 public GrapHtmlDemo(String title) {
22 JPanel panel = new JPanel(new BorderLayout(0, 5));
23 panel.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
24 targetField = new JTextField();
25 targetField.addActionListener(new TargetFieldAction());
26 tabbedPane = new JTabbedPane();
27 tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
28 tArea = new JTextArea();
29 tabbedPane.addTab("HTML-Code", new JScrollPane(tArea));
30
31 panel.add(createNorthPanel(), BorderLayout.NORTH);
32 panel.add(tabbedPane, BorderLayout.CENTER);
33 panel.add(createSouthPanel(), BorderLayout.SOUTH);
34
35 chooser = new JFileChooser();
36 FileNameExtensionFilter filter = new FileNameExtensionFilter(
37 "Plain Text", "txt", "html", "css", "js", "php", "java", "xml");
38 chooser.setFileFilter(filter);
39
40 frame = new JFrame(title);
41 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
42 frame.setSize(dim.width * 66 / 100, dim.height * 66 / 100);
43 frame.setLocationRelativeTo(null);
44 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
45 frame.add(panel);
46 frame.setVisible(true);
47 }
48
49 private JPanel createNorthPanel() {
50 JPanel panel = new JPanel(new BorderLayout(5, 0));
51 panel.add(new JLabel("URL:"), BorderLayout.WEST);
52 panel.add(targetField, BorderLayout.CENTER);
53 panel.add(new JButton(new TargetFieldAction("Los!")), BorderLayout.EAST);
54 return panel;
55 }
56
57 private JPanel createSouthPanel() {
58 JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
59 panel.add(new JButton(new StoreAction("Speichern")), BorderLayout.EAST);
60 return panel;
61 }
62
63 private void getContent() {
64 if(tabbedPane.getTabCount() > 1) {
65 tabbedPane.remove(1);
66 }
67 String urlString = targetField.getText().trim();
68 if(!urlString.startsWith("http")) {
69 urlString = "https://" + urlString;
70 targetField.setText(urlString);
71 }
72 try {
73 tArea.setText(null);
74 readContent(new URL(urlString));
75 }
76 catch(Exception ex) {
77 JTextArea eArea = new JTextArea(ex.toString());
78 eArea.setBackground(Color.PINK);
79 tabbedPane.addTab("Error", new JScrollPane(eArea));
80 tabbedPane.setSelectedIndex(1);
81 }
82 }
83
84 private void readContent(URL url) throws Exception {
85 Scanner scan = new Scanner(url.openStream());
86 if (scan != null) {
87 while(scan.hasNextLine()) {
88 tArea.append(scan.nextLine()+"\n\r");
89 }
90 }
91 }
92
93 private void storeContent() {
94 String text = tArea.getText();
95 if(!text.isEmpty()) {
96 int result = chooser.showSaveDialog(frame);
97 if(result == JFileChooser.APPROVE_OPTION) {
98 File file = chooser.getSelectedFile();
99 try {
100 BufferedWriter writer = new BufferedWriter(new FileWriter(file));
101 writer.write(text);
102 writer.flush();
103 writer.close();
104 } catch(IOException e) {
105 System.err.println(e);
106 JOptionPane.showMessageDialog(frame, e.getClass().getName(),
107 "ERROR: Fehler beim Schreiben der Datei auf den Datenträger!",
108 JOptionPane.ERROR_MESSAGE);
109 }
110 }
111 }
112 }
113
114 public static void main(String[] args) {
115 try {
116 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
117 }
118 catch(Exception e) {
119 System.err.println(e);
120 }
121
122 SwingUtilities.invokeLater(new Runnable() {
123 @Override
124 public void run() {
125 new GrapHtmlDemo("HTML-Code von Webseiten");
126 }
127 });
128 }
129
130 class TargetFieldAction extends AbstractAction {
131 TargetFieldAction() {
132 this(null);
133 }
134
135 TargetFieldAction(String text) {
136 super(text);
137 }
138
139 @Override
140 public void actionPerformed(ActionEvent e) {
141 getContent();
142 }
143 }
144
145 class StoreAction extends AbstractAction {
146 StoreAction(String text) {
147 super(text);
148 }
149
150 @Override
151 public void actionPerformed(ActionEvent e) {
152 storeContent();
153 }
154 }
155
156 }