javafxwebengine我可以';不要同时下载和加载页面
我正在尝试从网页下载文件,同时继续浏览同一网页javafxwebengine我可以';不要同时下载和加载页面,javafx,webview,download,httpurlconnection,webengine,Javafx,Webview,Download,Httpurlconnection,Webengine,我正在尝试从网页下载文件,同时继续浏览同一网页 public class Main extends Application { String urlBrowser = ""; @Override public void start(final Stage stage) { stage.setWidth(800); stage.setHeight(500); Scene scene = new Scene
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);
}
}
public类主扩展应用程序
{
字符串urlBrowser=“”;
@凌驾
公共作废开始(最后阶段){
舞台布景宽度(800);
舞台设置高度(500);
场景=新场景(新组());
最终WebView浏览器=新WebView();
final-WebEngine-WebEngine=browser.getEngine();
ScrollPane ScrollPane=新的ScrollPane();
scrollPane.setContent(浏览器);
ebEngine.locationProperty().addListener(新的ChangeListener()){
@凌驾
public void发生了变化(observeValue好吧,我尝试了ApacheHttpClient库,显然做了与HttpURLConnection相同的事情。
但是使用JSoup我能够实现我想要的。在下载过程中,我可以毫无问题地浏览网站。我使用post方法保留了用于从表单下载文件的代码,以防有人为您服务。谢谢
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) {}
}
可能尝试增加的值。默认值为5,因此可能没有帮助…感谢您的响应。我尝试增加连接数(我输入65000!)但它不起作用…在您完成下载文件之前,浏览器一直保持在同一页面上。也许如果我创建另一个webengine实例并在那里执行下载功能。但我重申,这非常奇怪,因为下载是在单独的线程上运行的。我不知道还有什么可以尝试的…尝试使用另一个库进行下载下载功能,如下图所示。WebView使用内部JDK URL连接,而apache http客户端则不使用,因此,如果WebView与自定义下载代码之间的JDK URL连接逻辑存在任何冲突,则在使用其他技术时不会出现冲突(当然,如果问题与其他问题相关,这不会解决问题,但值得一试).Hi jewelsea!我尝试了apache httpclient库,它成功了!!!问题是我在将Cookie从java.net.HttpURLConnection传递到apache的httpclient时遇到问题,因为有些下载是通过需要会话Cookie的post表单进行的…我试图用webEngine.load()加载另一个页面当下载开始时,我可以毫无问题地导航(谷歌、facebook、stackoverflow等),但它不会将我留在下载的网页上()。为什么会这样?