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