javafx-自动下载文章并将doc转换为docx

package sample.main.anli;

import com.jacob.com.ComThread;
import com.jacob.com.Variant;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.jsoup.Jsoup;
import org.w3c.dom.Document;

import java.io.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Timer;
import java.util.TimerTask;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.html.HTMLInputElement;

import javax.net.ssl.*;


public class getHtmlMain extends Application {

    private static WebView webView;
    private String finalS;

    final int[] count = {1};

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("cases collector");

        webView = new WebView();

        TextField tf = new TextField();

        Button bt = new Button("GO");
        Button fresh = new Button("fresh");
        Button start = new Button("start");

        Text tt = new Text();

        GridPane gp = new GridPane();
        Scene scene = new Scene(gp, 960, 600);

        gp.add(tf, 0, 0, 3, 1);

        gp.add(webView, 0, 1, 3, 3);
        gp.add(bt, 2, 5);
        gp.add(fresh, 1, 5);
        gp.add(start, 0, 5);

        gp.add(tt, 0, 4);

        fresh.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                webView.getEngine().load("https://xxxx");
                tf.setText("https://xxxxx");
            }
        });
        bt.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                webView.getEngine().load(tf.getText());
            }
        });
        start.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        File file = new File("D:\\xx\\contentid.txt");
                        FileReader fr = null;
                        try {
                            fr = new FileReader(file);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        BufferedReader br = new BufferedReader(fr);

                        String s = null;


                        while (true) {
                            try {
                                if (!((s = br.readLine()) != null)) break;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            finalS = s;
                            //key point here
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    tf.setText("https://sxxxx/" + finalS);
                                    System.out.println("https://xxxx/" + finalS);
                                    webView.getEngine().load("https://xxxxx/" + finalS);
                                }
                            });

                            count[0]++;
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });

        webView.getEngine().load("https://xxxxxx");
        tf.setText("https://sxxxxxxxx");



        webView.getEngine().documentProperty().addListener(new ChangeListener<Document>() {
            @Override
            public void changed(ObservableValue<? extends Document> observable, Document oldValue, Document newValue) {
                if(newValue!=null){
                    HTMLInputElement uid = (HTMLInputElement) newValue.getElementById("uid");
                    HTMLInputElement password = (HTMLInputElement) newValue.getElementById("password");
                    if (uid != null && password != null) {
                        uid.setValue("ccc");
                        password.setValue("bbbb");

                        Document doc = webView.getEngine().getDocument();
                        NodeList inputNodeList = doc.getElementsByTagName("input");
                        for (int i = 0; i < inputNodeList.getLength(); i++) {
                            NamedNodeMap attrs = inputNodeList.item(i).getAttributes();
                            if ("button".equals(attrs.getNamedItem("type").getTextContent())) {
                                Node inputnode = inputNodeList.item(i);
                                ((EventTarget) inputnode).addEventListener("click", new EventListener() {
                                    @Override
                                    public void handleEvent(Event evt) {
                                        if (uid != null) {
                                            try {
                                                webView.getEngine().executeScript("submitForm()");
                                            }catch (Exception e){
                                                System.err.println(e.getMessage());
                                            }

                                        }
                                    }
                                }, false);
                                HTMLInputElement buttonThis = (HTMLInputElement) inputNodeList.item(i);
                                buttonThis.click();
                            }
                        }
                    }
                }


                String html = (String) webView.getEngine().executeScript("document.documentElement.outerHTML");
                org.jsoup.nodes.Document doc1 = Jsoup.parse(html);

                String title = doc1.getElementsByTag("h1").html();
                title=title.replaceAll("\\\\","@");
                title=title.replaceAll("/","@");


                // 生成临时文件名称
                String fileName = title + ".doc";

                String bodyStr = null;

                bodyStr = doc1.getElementsByClass("case-content-left external-link-check").html();

                String content = "

" + title + "

"
+ bodyStr + ""; //将内容转成数组 byte b[] = new byte[0]; try { b = content.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //使用的节点流 ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem poifs = new POIFSFileSystem(); DirectoryEntry directory = poifs.getRoot(); try { DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); } catch (IOException e) { e.printStackTrace(); } FileOutputStream ostream = null; try { fileName=fileName.replaceAll("\\\\","@"); fileName=fileName.replaceAll("/","@"); ostream = new FileOutputStream("D:\\xx\\public3\\" + fileName); } catch (FileNotFoundException e) { System.err.println(tf.getText()); e.printStackTrace(); } try { poifs.writeFilesystem(ostream); } catch (IOException e) { e.printStackTrace(); } try { bais.close(); } catch (IOException e) { e.printStackTrace(); } try { ostream.close(); } catch (IOException e) { e.printStackTrace(); } primaryStage.setTitle(fileName); title=title.replaceAll("\\\\","@"); title=title.replaceAll("/","@"); docToDOcx("D:\\xx\\public3\\" + title + ".doc", "D:\\xx\\public3\\" + title + ".docx"); tt.setText(count[0] + "/180"); } }); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; // Install the all-trusting trust manager SSLContext sc = null; try { sc = SSLContext.getInstance("SSL"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } try { sc.init(null, trustAllCerts, new java.security.SecureRandom()); } catch (KeyManagementException e) { e.printStackTrace(); } HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // Create all-trusting host name verifier HostnameVerifier allHostsValid = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; // Install the all-trusting host verifier HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); launch(args); } public static void docToDOcx(String path, String outPath) { com.spire.doc.Document dc = new com.spire.doc.Document(); dc.loadFromFile(path); dc.saveToFile(outPath, FileFormat.Docx_2013); dc.close(); } }

你可能感兴趣的:(javafx,java,java)