黄缙华

黄缙华的博客

他的个人主页  他的博客

关于使用batik中出现“unable to make sense of URL for connection”的问题

黄缙华  2009年11月03日 星期二 15:29 | 5342次浏览 | 0条评论

使用基于java的batik来打开SVG文件时,出现了“unable to make sense of URL for connection”的问题。现在将问题的解决办法写下来,做个笔记,以便自己以后再用。

使用batik来打开SVG文件时,用到如下代码来读取SVG文件:

JFrame frame;
    JSVGCanvas canvas;
    Document document;
    Window window;

    public SVGApplication() {
        frame = new JFrame();
        canvas = new JSVGCanvas();
        // Forces the canvas to always be dynamic even if the current
        // document does not contain scripting or animation.
        canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        canvas.addSVGLoadEventDispatcherListener
            (new SVGLoadEventDispatcherAdapter() {
                    public void svgLoadEventDispatchStarted
                        (SVGLoadEventDispatcherEvent e) {
                        // At this time the document is available...
                        document = canvas.getSVGDocument();
                        // ...and the window object too.
                        window = canvas.getUpdateManager().
                            getScriptingEnvironment().createWindow();
                        // Registers the listeners on the document
                        // just before the SVGLoad event is
                        // dispatched.
                        registerListeners();
                        // It is time to pack the frame.
                        frame.pack();
                    }
                });
       
        frame.addWindowListener(new WindowAdapter() {
                public void windowOpened(WindowEvent e) {
                    // The canvas is ready to load the base document
                    // now, from the AWT thread.
                    canvas.setURI("line.svg");                  
                }
            });

其中由于红色部分出错,引起了“unable to make sense of URL for connection”。这说明这个路径不可用。经过一翻折腾以后发现,应该改成:

canvas.setURI("file:/home/hjh/svg/line.svg");

这样,就可以读取了。开始不知道为什么这样就可以了。后来分析后发现,只“/home/hjh/svg/line.svg”这个是表示的操作系统的路径,而不是一个URL路径。必须要在前面加上“file:/”才行。WINDOWS下的也是如此。如果SVG文件存在C:/下。不能直接用“C:/line.svg”,而应该用“file:/C:/line.svg”才可以。

 

 

 

评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !

暂时没有评论

Zeuux © 2024

京ICP备05028076号