Webseiten herunterladen und speichern

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen

Folgender 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");
 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 readContent(URL url) throws Exception {
 64       Scanner scan = new Scanner(url.openStream());
 65       if (scan != null) {
 66          while(scan.hasNextLine()) {
 67             tArea.append(scan.nextLine()+"\n\r");
 68          }
 69       }
 70    }
 71 
 72    private void storeContent() {
 73       String text = tArea.getText();
 74       if(!text.isEmpty()) {
 75          int result = chooser.showSaveDialog(frame);
 76          if(result == JFileChooser.APPROVE_OPTION) {
 77             File file = chooser.getSelectedFile();
 78             try {
 79                BufferedWriter writer = new BufferedWriter(new FileWriter(file));         
 80                writer.write(text);
 81                writer.flush();
 82                writer.close();
 83             } catch(IOException e) {
 84                System.err.println(e);
 85                JOptionPane.showMessageDialog(frame, e.getClass().getName(),
 86                   "ERROR: Fehler beim Schreiben der Datei auf den Datenträger!",
 87                   JOptionPane.ERROR_MESSAGE);
 88             }
 89          }
 90       }
 91    }
 92 
 93    public static void main(String[] args) {
 94       try {
 95          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 96       }
 97       catch(Exception e) {
 98          System.err.println(e);
 99       }
100 
101       SwingUtilities.invokeLater(new Runnable() {
102          @Override
103          public void run() {
104             new GrapHtmlDemo("HTML-Code von Webseiten");
105          }
106       });
107    }
108 
109    class TargetFieldAction extends AbstractAction {
110       TargetFieldAction() {
111          this(null);
112       }
113       
114       TargetFieldAction(String text) {
115          super(text);
116       }
117       
118       @Override
119       public void actionPerformed(ActionEvent e) {
120          if(tabbedPane.getTabCount() > 1) {
121             tabbedPane.remove(1);
122          }
123          String urlString = targetField.getText().trim();
124          if(!urlString.startsWith("http")) {
125             urlString = "https://" + urlString;
126             targetField.setText(urlString);
127          }
128          try {
129             tArea.setText(null);
130             readContent(new URL(urlString));
131          }
132          catch(Exception ex) {
133             JTextArea eArea = new JTextArea(ex.toString());
134             eArea.setBackground(Color.PINK);
135             tabbedPane.addTab("Error", new JScrollPane(eArea));
136             tabbedPane.setSelectedIndex(1);
137          }
138       }
139    }
140 
141    class StoreAction extends AbstractAction {
142       StoreAction(String text) {
143          super(text);
144       }
145       
146       @Override
147       public void actionPerformed(ActionEvent e) {
148          storeContent();
149       }
150    }
151 
152 }