Webseiten herunterladen und speichern: Unterschied zwischen den Versionen

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen
K (L-ectron-X verschob die Seite Webseiten speichern nach Webseiten herunterladen und speichern, ohne dabei eine Weiterleitung anzulegen)
K
 
(8 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
Folgender vollständiger Code zeigt, wie man Webseiten herunterladen und speichern kann:
+
[[Datei:GrapHTML.png|mini]]
 +
Folgender, sehr einfacher, vollständiger Code zeigt, wie man Webseiten in ein Java-Programm herunterladen und auf der Festplatte speichern kann:
  
 
<syntaxhighlight lang="java" line="true">
 
<syntaxhighlight lang="java" line="true">
Zeile 10: Zeile 11:
 
import javax.swing.filechooser.*;
 
import javax.swing.filechooser.*;
  
 +
/**
 +
* GrapHtmlDemo zeigt, wie man mit Java Quelltext von Webseiten herunterladen und speichern kann.
 +
*
 +
* @author Gernot Segieth
 +
*/
 
public class GrapHtmlDemo {
 
public class GrapHtmlDemo {
 
   private final JFrame frame;
 
   private final JFrame frame;
Zeile 33: Zeile 39:
 
       chooser = new JFileChooser();
 
       chooser = new JFileChooser();
 
       FileNameExtensionFilter filter = new FileNameExtensionFilter(
 
       FileNameExtensionFilter filter = new FileNameExtensionFilter(
         "Plain Text", "txt", "html", "css", "js");
+
         "Plain Text", "txt", "html", "css", "js", "php", "java", "xml");
 
       chooser.setFileFilter(filter);
 
       chooser.setFileFilter(filter);
 
        
 
        
Zeile 58: Zeile 64:
 
       return panel;
 
       return panel;
 
   }
 
   }
 +
 +
  private void getContent() {
 +
      if(tabbedPane.getTabCount() > 1) {
 +
        tabbedPane.remove(1);
 +
      }
 +
      String urlString = targetField.getText().trim();
 +
      if(!urlString.startsWith("http")) {
 +
        urlString = "https://" + urlString;
 +
        targetField.setText(urlString);
 +
      }
 +
      try {
 +
        tArea.setText(null);
 +
        readContent(new URL(urlString));
 +
      }
 +
      catch(Exception ex) {
 +
        JTextArea eArea = new JTextArea(ex.toString());
 +
        eArea.setBackground(Color.PINK);
 +
        tabbedPane.addTab("Error", new JScrollPane(eArea));
 +
        tabbedPane.setSelectedIndex(1);
 +
      }
 +
  }
  
 
   private void readContent(URL url) throws Exception {
 
   private void readContent(URL url) throws Exception {
Zeile 63: Zeile 90:
 
       if (scan != null) {
 
       if (scan != null) {
 
         while(scan.hasNextLine()) {
 
         while(scan.hasNextLine()) {
             tArea.append(scan.nextLine()+"\n\r");
+
             tArea.append(scan.nextLine());
 +
            tArea.append("\n");
 
         }
 
         }
 
       }
 
       }
Zeile 69: Zeile 97:
  
 
   private void storeContent() {
 
   private void storeContent() {
       int result = chooser.showSaveDialog(frame);
+
       String text = tArea.getText();
      if(result == JFileChooser.APPROVE_OPTION) {
+
      if(!text.isEmpty()) {
        File file = chooser.getSelectedFile();
+
        int result = chooser.showSaveDialog(frame);
        try {
+
        if(result == JFileChooser.APPROVE_OPTION) {
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));         
+
            File file = chooser.getSelectedFile();
            writer.write(tArea.getText());
+
            try {
            writer.flush();
+
              BufferedWriter writer = new BufferedWriter(new FileWriter(file));         
            writer.close();
+
              writer.write(text);
        } catch(IOException e) {
+
              writer.flush();
            System.err.println(e);
+
              writer.close();
            JOptionPane.showMessageDialog(frame, e.getClass().getName(),
+
            } catch(IOException e) {
              "ERROR: Fehler beim Schreiben der Datei auf den Datenträger!",
+
              System.err.println(e);
              JOptionPane.ERROR_MESSAGE);
+
              JOptionPane.showMessageDialog(frame, e.getClass().getName(),
 +
                  "ERROR: Fehler beim Schreiben der Datei auf den Datenträger!",
 +
                  JOptionPane.ERROR_MESSAGE);
 +
            }
 
         }
 
         }
 
       }
 
       }
Zeile 113: Zeile 144:
 
       @Override
 
       @Override
 
       public void actionPerformed(ActionEvent e) {
 
       public void actionPerformed(ActionEvent e) {
         if(tabbedPane.getTabCount() > 1) {
+
         getContent();
            tabbedPane.remove(1);
 
        }
 
        String urlString = targetField.getText().trim();
 
        if(!urlString.startsWith("http")) {
 
            urlString = "https://" + urlString;
 
            targetField.setText(urlString);
 
        }
 
        try {
 
            tArea.setText(null);
 
            readContent(new URL(urlString));
 
        }
 
        catch(Exception ex) {
 
            JTextArea eArea = new JTextArea(ex.toString());
 
            eArea.setBackground(Color.PINK);
 
            tabbedPane.addTab("Error", new JScrollPane(eArea));
 
            tabbedPane.setSelectedIndex(1);
 
        }
 
 
       }
 
       }
 
   }
 
   }
Zeile 148: Zeile 162:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
{{Fragen stellen}}
  
 
[[Kategorie:Java-Codeschnipsel]]
 
[[Kategorie:Java-Codeschnipsel]]

Aktuelle Version vom 25. Juli 2019, 07:59 Uhr

GrapHTML.png

Folgender, sehr einfacher, vollständiger Code zeigt, wie man Webseiten in ein Java-Programm herunterladen 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 }


Fragen

Das Thema wurde nicht ausreichend behandelt? Du hast Fragen dazu und brauchst weitere Informationen? Lass Dir von uns helfen!

Wir helfen dir gerne!


Dir hat dieser Artikel gefallen? Oder Du hast Fehler entdeckt und möchtest zur Berichtigung beitragen? Prima! Schreibe einen Kommentar!

Du musst angemeldet sein, um einen Kommentar abzugeben.