3 Replies Latest reply: Mar 15, 2013 1:11 AM by 996871 RSS

    I use javaFx WebViewBrowser to download ZIP file, the page no action

    996871
      I use javaFx WebViewBrowser to download ZIP file, the page no action; other tools example chrome ,ie can download the zip file .

      so can you writer a download zip file example for me ?

      thanks ,my english is so bad ,sorry !!! :)
        • 1. Re: I use javaFx WebViewBrowser to download ZIP file, the page no action
          jsmith
          WebView doesn't have a built in file downloader - you have to implement it yourself.

          You can find a sample implementation for file downloads in JavaFX here =>
          http://www.zenjava.com/2011/11/14/file-downloading-in-javafx-2-0-over-http/

          That sample does not include the hooks into WebView to trigger the downloads.
          You will need to trigger off the mime type or the file extension in the url.

          A rudimentary sample trigger a download off of webview is provided in the code below.
          For a nice solution, you would probably want to spin the downloader off with it's own UI similar and manage the download itself as a JavaFX Task similar to how the zenjava component works.
          Code adapted from => http://code.google.com/p/willow-browser/source/browse/src/main/java/org/jewelsea/willow/BrowserWindow.java
          // monitor the location url, and if it is a pdf file, then create a pdf viewer for it, if it is downloadable, then download it.
          view.getEngine().locationProperty().addListener(new ChangeListener<String>() {
            @Override public void changed(ObservableValue<? extends String> observableValue, String oldLoc, String newLoc) {
              if (newLoc.endsWith(".pdf")) {
                try {
                  final PDFViewer pdfViewer = new PDFViewer(false);  // todo try icepdf viewer instead...
                  pdfViewer.openFile(new URL(newLoc));
                } catch (Exception ex) {
                  // just fail to open a bad pdf url silently - no action required.
                }
              }
              String downloadableExtension = null;  // todo I wonder how to find out from WebView which documents it could not process so that I could trigger a save as for them?
              String[] downloadableExtensions = { ".doc", ".xls", ".zip", ".tgz", ".jar" };
              for (String ext: downloadableExtensions) {
                if (newLoc.endsWith(ext)) {
                  downloadableExtension = ext;
                  break;
                }
              }
              if (downloadableExtension != null) {  
                // create a file save option for performing a download.
                FileChooser chooser = new FileChooser();
                chooser.setTitle("Save " + newLoc);
                chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Downloadable File", downloadableExtension));
                int filenameIdx = newLoc.lastIndexOf("/") + 1;
                if (filenameIdx != 0) {
                  File saveFile = chooser.showSaveDialog(view.getScene().getWindow());
          
                  if (saveFile != null) {
                    BufferedInputStream  is = null;
                    BufferedOutputStream os = null;
                    try {
                      is = new BufferedInputStream(new URL(newLoc).openStream());
                      os = new BufferedOutputStream(new FileOutputStream(saveFile));
                      int b = is.read();
                      while (b != -1) {
                        os.write(b);
                        b = is.read();
                      }
                    } catch (FileNotFoundException e) {
                      System.out.println("Unable to save file: " + e);
                    } catch (MalformedURLException e) {
                      System.out.println("Unable to save file: " + e);
                    } catch (IOException e) {
                      System.out.println("Unable to save file: " + e);
                    } finally {
                      try { if (is != null) is.close(); } catch (IOException e) { /** no action required. */ }
                      try { if (os != null) os.close(); } catch (IOException e) { /** no action required. */ }
                    }
                  }
                     
                  // todo provide feedback on the save function and provide a download list and download list lookup.
                }
              }
            }
          });
          • 2. Re: I use javaFx WebViewBrowser to download ZIP file, the page no action
            996871
            thanks for jsmith !

            so next question : I use locationProperty().addListener ,but in the web system,it only listen userLogin.action and userLoginout.action, the other action the listener can't listen.

            the web like : http://data.ccxe.com.cn
            user:zhangzy9
            password:111111

            Edited by: user12966970 on 2013-3-14 下午9:58