javafx-以窗口的形式定时访问指定地址保存为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.*;

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://00087919");
                tf.setText("https://0087919");
            }
        });
        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:\\dayu\\contentid.txt");
                        FileReader fr = null;
                        try {
                            fr = new FileReader(file);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        BufferedReader br = new BufferedReader(fr);

                        String s = null;

                        Map<String,Integer> repeat=new HashMap<>();

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

                            finalS = s;

                            if(null==repeat.get(s)){
                                repeat.put(finalS,0);
                            }else{
                                continue;
                            }


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

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

        webView.getEngine().load("https://100087919");
        tf.setText("https://0087919");



        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("77");
                        password.setValue("!aa");

                        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").text();
//                String title = doc1.getElementsByClass("knowledgebase-title").html();
                title=title.replaceAll("\\\\","@");
                title=title.replaceAll("/","@");


                // 生成临时文件名称
                String fileName = title=="" ? new Random().nextInt(5000)+"" : title + ".doc";

                //todo 这里有bug,以后修改html中有多个h1的处理,如这个链接:https://xxx.com/enterprise/zh/kno/087246
                System.out.println("title:"+title);
                System.out.println("临时文件名:"+fileName);

                String bodyStr = null;

                bodyStr = doc1.getElementsByClass("case-content-box").html();

                String content = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body><h3>" + title + "</h3>" + bodyStr + "</body></html>";
                //将内容转成数组
                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:\\dayu\\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:\\dayu\\public3\\" + title + ".doc", "D:\\dayu\\public3\\" + title + ".docx");


                tt.setText(count[0] + "/257");

            }
        });

        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-Support是一个开源项目,致力于为JavaFX提供友好的支持和工具。JavaFX是一个用于构建丰富用户界面的Java库,而JavaFX-Support项目旨在为开发人员提供便利和增强功能,以提高他们在开发JavaFX应用程序时的效率和体验。 JavaFX-Support项目具有以下特点和功能: 1.样式编辑器:JavaFX-Support项目提供了一个样式编辑器,使开发人员能够轻松编辑和定制JavaFX应用程序的视觉样式。通过这个编辑器,开发人员可以直观地调整应用程序的颜色、字体和其他视觉元素,而无需手动编写代码。 2.视觉组件库:JavaFX-Support项目还包含了一个丰富的视觉组件库,开发人员可以直接在应用程序中使用这些组件,而无需自己编写。这些组件可以帮助开发人员快速构建出现频率较高的界面元素,例如按钮、文本框、列表等。 3.布局助手:JavaFX-Support项目还提供了一个布局助手,用于在JavaFX应用程序中进行布局。开发人员可以方便地使用拖放方式调整和排列组件,从而更加灵活地设计界面。 4.调试工具:JavaFX-Support项目还包含了一些调试工具,用于帮助开发人员在开发过程中定位和修复问题。这些工具可以帮助开发人员追踪布局问题、排查异常和提高性能。 总的来说,JavaFX-Support项目是一个强大而实用的工具集,它使得开发人员可以更加便捷地开发和定制JavaFX应用程序。无论是初学者还是有经验的开发人员都可以从中受益,并提高他们在JavaFX开发中的效率和质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值