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