You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Batik加载SVG文件失败,请求技术排查支持

使用Batik加载SVG文件时遇到两类异常求助

我尝试使用Batik加载并显示SVG文件,但两种尝试方法均出现不同异常。双击SVG文件可正常查看,但代码无法加载显示,已尝试多个Batik加载SVG的教程均无效。预期实现SVG加载并绘制到JFrame的画布上,代码中可能存在测试残留代码,恳请协助排查问题。

相关代码

public static SVGGraphics2D viewSVG()
{
    File file = findFile();
    DOMImplementation imp = SVGDOMImplementation.getDOMImplementation();
    //SAXSVGDocumentFactory
    SAXSVGDocumentFactory fact = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
    JSVGCanvas canvas = new JSVGCanvas();
    SVGDocument doc = null;
    SVGGraphics2D g = null;
    try {
        doc = fact.createSVGDocument(file.getAbsolutePath());
        g = new SVGGraphics2D(doc);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        doc = (SVGDocument) imp.createDocument(file.getAbsolutePath(), "svg", null);
        e1.printStackTrace();
    }
    doc.getRootElement();
    g.setSVGCanvasSize(new Dimension(1000, 1200));
    JFrame frame = new JFrame(doc.getLocalName());
    Element root = doc.getDocumentElement();
    g.getRoot(root);
    canvas.paint(g);
    try {
        Writer out = new FileWriter(System.getProperty("user.dir") + "/tst1.svg");
        //g.stream(out, true);
        g.stream(file.getAbsolutePath());
        canvas.paint(g);
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    frame.getContentPane().add(canvas);
    System.out.println(doc.getDocumentElement().getClass());
    System.out.println(doc.getRootElement());
    canvas.setSVGDocument(doc);
    frame.pack();
    frame.setVisible(true);
    return g;
}
/*
 * @param find = JFileChooser object to allow user to navigate file explorer
 * @param result = integer indicating if the selection was valid
 * @param@return file = file selected by user
 * uses JFileChooser to allow user to select a file and then return the file if it is valid or null if it is not
 */
public static File findFile()
{
    JFileChooser find = new JFileChooser();
    find.setCurrentDirectory(new File(System.getProperty("user.dir")));
    int result = find.showOpenDialog(find);
    if(JFileChooser.APPROVE_OPTION == result)
    {
        File file = find.getSelectedFile();
        return file;
    }
    else
    {
        return null;
    }
}

抛出的异常

异常1

java.io.IOException: Unable to make sense of URL for connection
    at org.apache.batik.util.ParsedURLData.openStreamInternal(ParsedURLData.java:515)
    at org.apache.batik.util.ParsedURLData.openStream(ParsedURLData.java:476)
    at org.apache.batik.util.ParsedURL.openStream(ParsedURL.java:440)
    at org.apache.batik.anim.dom.SAXSVGDocumentFactory.createDocument(SAXSVGDocumentFactory.java:159)
    at org.apache.batik.anim.dom.SAXSVGDocumentFactory.createSVGDocument(SAXSVGDocumentFactory.java:124)
    at calendarmaker.Objects.Pane.viewSVG(Pane.java:118)

异常2

java.lang.ClassCastException: class org.apache.batik.dom.GenericElementNS cannot be cast to class org.w3c.dom.svg.SVGSVGElement (org.apache.batik.dom.GenericElementNS and org.w3c.dom.svg.SVGSVGElement are in unnamed module of loader 'app')
    at org.apache.batik.anim.dom.SVGOMDocument.getRootElement(SVGOMDocument.java:234)
    at calendarmaker.Objects.Pane.viewSVG(Pane.java:125)

内容的提问来源于stack exchange,提问作者Noah Gaston

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:50:00