Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)
JxBrowser allows you to save web pages as a file or set of files. You can use Browser.saveWebPage(String filePath, String dirPath, SavePageType saveType) method to save the current web page. Before saving make sure the page is loaded completely.(JxBrowser允许您将网页另存为一个文件或一组文件。您可以使用Browser.saveWebPage(String filePath,String dirPath,SavePageType saveType)方法来保存当前网页。保存之前,请确保页面已完全加载。)
String filePath = "C:\\SavedPages\\index.html"; String dirPath = "C:\\SavedPages\\resources"; browser.saveWebPage(filePath, dirPath, SavePageType.COMPLETE_HTML);
import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.SavePageType; import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent; import com.teamdev.jxbrowser.chromium.events.LoadAdapter; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; /** * This sample demonstrates how to save the loaded web page. */ public class SaveWebPageSample { public static void main(String[] args) { Browser browser = new Browser(); BrowserView browserView = new BrowserView(browser); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(browserView, BorderLayout.CENTER); frame.setSize(700, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); browser.addLoadListener(new LoadAdapter() { @Override public void onFinishLoadingFrame(FinishLoadingEvent event) { if (event.isMainFrame()) { String filePath = "D:\\Test\\index.html"; String dirPath = "D:\\Test\\resources"; event.getBrowser().saveWebPage(filePath, dirPath, SavePageType.COMPLETE_HTML); } } }); browser.loadURL("http://www.google.com"); } }