我的JEditorPane有问题,不能加载网址,总是显示java.io.FileNotFoundException。我完全搞不懂如何解决这个问题。
JEditorPane editorpane = new JEditorPane();
editorpane.setEditable(false);
String backslash="\\";
String itemcode="a91000mf";
int ctr=6;
File file = new File("file:///C:/Development/project2/OfflineSales/test/index.html?item_code="+itemcode+"&jumlah="+String.valueOf(ctr)+"&lokasi=../images");
if (file != null) {
try {
//editorpane.addPropertyChangeListener(propertyName, listener)
editorpane.setPage(file.toURL());
System.out.println(file.toString());
} catch (IOException e) {
System.err.println(e.toString());
}
} else {
System.err.println("Couldn't find file: TextSamplerDemoHelp.html");
}我刚放入"file:///C:/Development/project2/OfflineSales/test/index.html?item_code="+itemcode",但它会显示同样的错误:无法打开文件,但我可以在浏览器中打开它
发布于 2015-05-21 11:40:33
File需要本地文件路径,但"file://....“”是一个URI...所以试试这个:
URI uri = new URI("file:///C:/Development/project2/OfflineSales/test/index.html?item_code="+itemcode+"&jumlah="+String.valueOf(ctr)+"&lokasi=../images");
File file = new File(uri);发布于 2015-05-22 10:54:55
您应该删除File类的所有用法。
以" file :“开头的字符串是一个URL,而不是一个文件名。它不是File构造函数的有效参数。
您正在调用的JEditor.setPage方法接受一个网址,而不是一个文件。没有理由创建File实例:
try {
URL url = new URL("file:///C:/Development/project2/OfflineSales/test/index.html?item_code=" + itemcode + "&jumlah=" + ctr + "&lokasi=../images");
editorpane.setPage(url);
} catch (IOException e) {
e.printStackTrace();
}JEditorPane还提供了一个方便的方法,可以将字符串转换为URL,因此您甚至可以完全跳过URL类的使用:
String url = "file:///C:/Development/project2/OfflineSales/test/index.html?item_code=" + itemcode + "&jumlah=" + ctr + "&lokasi=../images";
try {
editorpane.setPage(url);
} catch (IOException e) {
e.printStackTrace();
}(请注意,不需要String.valueOf。只要您将字符串与任何对象或原始值连接起来,它就会被隐式调用。)
https://stackoverflow.com/questions/30364070
复制相似问题