无法使URL的意义连接读取SVG文件

问题描述 投票:0回答:2

我有一个春天启动v2.1.2.RELEASE应用。我有../src/main/resources/icons/128/black/ae.png文件

我想阅读,但我得到了一个错误:无法向URL的意义连接

@SpringBootApplication
public class SvgManagerApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SvgManagerApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {      

        try {

            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            Document doc = f.createDocument("classpath:icons/128/black/ae.svg");

            System.out.println(doc);

        } catch (IOException ex) {

            System.out.println(ex.getMessage());

        }
    }
}
java spring spring-boot svg batik
2个回答
0
投票

你是混合两种不同的框架;而classpath:似乎与蜡染(SAXSVGDocumentFactoryhttps://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/anim/dom/SAXSVGDocumentFactory.html有关春天

你可以这样做:

@SpringBootApplication
public class SvgManagerApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SvgManagerApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {      

        try {
        Resource svg = new ClassPathResource("icons/128/black/ae.png"); 
            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            Document doc = f.createDocument(SVG_DOCUMENT_URI, svg.getInputStream());

            System.out.println(doc);

        } catch (IOException ex) {

            System.out.println(ex.getMessage());

        }
    }
}

有关资源的更多信息可以在这里找到https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/Resource.html,而关于使用ClassPathResource更多信息可以在这里找到https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html


1
投票

您可以尝试让这样的路径:

String path = SvgManagerApplication.class.getClassLoader().getResource("icons/128/black/ae.svg").getPath();
© www.soinside.com 2019 - 2024. All rights reserved.