Webseiten herunterladen und speichern
Aus Byte-Welt Wiki
Version vom 10. Juni 2019, 08:05 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());
89 tArea.append("\n");
90 }
91 }
92 }
93
94 private void storeContent() {
95 String text = tArea.getText();
96 if(!text.isEmpty()) {
97 int result = chooser.showSaveDialog(frame);
98 if(result == JFileChooser.APPROVE_OPTION) {
99 File file = chooser.getSelectedFile();
100 try {
101 BufferedWriter writer = new BufferedWriter(new FileWriter(file));
102 writer.write(text);
103 writer.flush();
104 writer.close();
105 } catch(IOException e) {
106 System.err.println(e);
107 JOptionPane.showMessageDialog(frame, e.getClass().getName(),
108 "ERROR: Fehler beim Schreiben der Datei auf den Datenträger!",
109 JOptionPane.ERROR_MESSAGE);
110 }
111 }
112 }
113 }
114
115 public static void main(String[] args) {
116 try {
117 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
118 }
119 catch(Exception e) {
120 System.err.println(e);
121 }
122
123 SwingUtilities.invokeLater(new Runnable() {
124 @Override
125 public void run() {
126 new GrapHtmlDemo("HTML-Code von Webseiten");
127 }
128 });
129 }
130
131 class TargetFieldAction extends AbstractAction {
132 TargetFieldAction() {
133 this(null);
134 }
135
136 TargetFieldAction(String text) {
137 super(text);
138 }
139
140 @Override
141 public void actionPerformed(ActionEvent e) {
142 getContent();
143 }
144 }
145
146 class StoreAction extends AbstractAction {
147 StoreAction(String text) {
148 super(text);
149 }
150
151 @Override
152 public void actionPerformed(ActionEvent e) {
153 storeContent();
154 }
155 }
156
157 }