Javafx webengine 我无法同时下载和加载页面

Javafx webengine I can't download and load a page at the same time

我正在尝试从网页下载文件,同时继续浏览同一网页。

    public class Main extends Application
     {

    String urlBrowser = "";

    @Override
    public void start(final Stage stage) {
        stage.setWidth(800);
        stage.setHeight(500);
        Scene scene = new Scene(new Group());

        final WebView browser = new WebView();
        final WebEngine webEngine = browser.getEngine();

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(browser);

        ebEngine.locationProperty().addListener(new ChangeListener<String>() {
        @Override 
        public void changed(ObservableValue<? extends String> observableValue, String oldLoc, String newLoc) {
            url = observableValue.getValue());
        }
        });

        webEngine.getLoadWorker().stateProperty()
            .addListener(new ChangeListener<State>() {

            @Override
            public void changed(ObservableValue ov, State oldState, State newState) {
                if (newState == Worker.State.CANCELLED) {
                    if(urlBrowser.contains("download"){
                        try{
                            Download download = new Download(webEngine.getLocation());
                            Thread t = new Thread(download);
                            t.start();
                        }catch(Exception ex){
                            ex.printStackTrace();
                        }
                    }
                }
            });
            webEngine.load("http://www.website.com");
            scene.setRoot(scrollPane);

            stage.setScene(scene);
            stage.show();
        }

        public static void main(String[] args) {
            launch(args);
        }
    }
}


    public class Download implements Runnable{

    private final String urlPath;
    private HttpURLConnection connection;

    public Download(String url) {
        this.urlPath = url;
    }

    @Override
    public void run() {
        try {
            URL url = new URL(urlPath);
            connection = (HttpURLConnection) url.openConnection();
            configConnection();
            download();
        }catch (MalformedURLException ex) {
            ex.printStackTrace();
        }catch (IOException ex) {
            ex.printStackTrace();
        }finally{
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    private void configConnection(){
        try {
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0");
            connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            connection.setReadTimeout(0);
            connection.setDoOutput(true);
            try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
                wr.writeBytes(parametrosFormulario);
                wr.flush();
            }
        }catch (ProtocolException ex) {
            Logger.getLogger(DownloadFormPost.class.getName()).log(Level.SEVERE, null, ex);
        }catch (IOException ex) {
             Logger.getLogger(DownloadFormPost.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void download(){
        String contentDisposition = connection.getHeaderField("Content-Disposition");
        String url = connection.getURL().getHost();
        String saveFilePath = getFileName("src/test", contentDisposition, url);

        FileOutputStream outputStream = null;
        InputStream inputStream = null;
        try{
            outputStream = new FileOutputStream(saveFilePath);
            inputStream = connection.getInputStream();
            int BUFFER_SIZE = 10240;
            int bytesRead = -1;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((bytesRead = inputStream.read(buffer)) != -1){
                outputStream.write(buffer, 0, bytesRead);
            }
        }catch(FileNotFoundException ex){
            ex.printStackTrace();
        }catch (IOException ex) {
            ex.printStackTrace();
        }finally{
            try { if (inputStream != null) inputStream.close(); } catch (IOException e) {}
            try { if (outputStream != null) outputStream.close(); } catch (IOException e) {}
        }
    }

    private String getFileName(String path, String contentDisposition, String url){
        String fileName = "";
        if (contentDisposition != null) {
            int index = contentDisposition.indexOf("filename=");
            if (index > 0) {
                fileName = contentDisposition.substring(index + 9, contentDisposition.length()).replace("\"", "");
            }
        } else {
            fileName = url.substring(url.lastIndexOf("/") + 1,
            url.length());
        }
        return (path + File.separator + fileName);
    }
}

进入我想要的网站后,我导航到下载 link,然后单击,程序开始下载。到目前为止一切顺利,问题是下载文件很重,我在下载时想继续浏览页面。当我点击 link 时,浏览器什么都不做,下载完成后转到我点击的 link。 我不太清楚发生了什么,因为下载我放在一个单独的线程中,但浏览器似乎不关心并且不会让我继续浏览网站,直到我完成下载。

How can I free the download of the navigation through the web page?

谢谢!

好吧,我尝试了 Apache HttpClient 库,并且毫无疑问地做了与 HttpURLConnection 相同的事情。 但是使用 JSoup 我能够实现我想要的。下载过程中,我可以毫无问题地浏览网站。我留下了我用来使用 post 方法从表单下载文件的代码,以防有人为您服务。谢谢jewelsea!

Response response = Jsoup.connect(url)
                    .method(Connection.Method.POST)
                    .ignoreContentType(true)
                    .timeout(0)
                    .data("user","user")
                    .data("pass","pass")
                    .execute();

String saveFilePath = "file.zip";
FileOutputStream outputStream = null;
InputStream inputStream = null;
try{
    outputStream = new FileOutputStream(saveFilePath);
    inputStream = new ByteArrayInputStream(ByteBuffer.wrap(response.bodyAsBytes()).array());
    int BUFFER_SIZE = 1024;
    int bytesRead = -1;
    byte[] buffer = new byte[BUFFER_SIZE];
    while ((bytesRead = inputStream.read(buffer)) != -1)
        outputStream.write(buffer, 0, bytesRead);
}catch(FileNotFoundException ex){
    ex.printStackTrace();
}catch (IOException ex) {
    ex.printStackTrace();
} catch (Exception ex) {
    ex.printStackTrace();
}finally{
    try { if (inputStream != null) inputStream.close(); } catch (IOException e) {}
    try { if (outputStream != null) outputStream.close(); } catch (IOException e) {}
}