首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JEditorpane无法加载URL

JEditorpane无法加载URL
EN

Stack Overflow用户
提问于 2015-05-21 11:33:44
回答 2查看 218关注 0票数 2

我的JEditorPane有问题,不能加载网址,总是显示java.io.FileNotFoundException。我完全搞不懂如何解决这个问题。

代码语言:javascript
复制
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",但它会显示同样的错误:无法打开文件,但我可以在浏览器中打开它

EN

回答 2

Stack Overflow用户

发布于 2015-05-21 11:40:33

File需要本地文件路径,但"file://....“”是一个URI...所以试试这个:

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2015-05-22 10:54:55

您应该删除File类的所有用法。

以" file :“开头的字符串是一个URL,而不是一个文件名。它不是File构造函数的有效参数。

您正在调用的JEditor.setPage方法接受一个网址,而不是一个文件。没有理由创建File实例:

代码语言:javascript
复制
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类的使用:

代码语言:javascript
复制
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。只要您将字符串与任何对象或原始值连接起来,它就会被隐式调用。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30364070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档