为了账号安全,请及时绑定邮箱和手机立即绑定

无法理解用于连接读取 svg 文件的 URL

无法理解用于连接读取 svg 文件的 URL

慕盖茨4494581 2022-06-04 16:46:31
我有一个 Spring Boot v2.1.2.RELEASE 应用程序。我有一个文件 ../src/main/resources/icons/128/black/ae.png我想阅读,但出现错误:无法理解连接的 URL@SpringBootApplicationpublic 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());        }    }}
查看完整描述

2 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

您可以尝试通过这种方式获取路径:

String path = SvgManagerApplication.class.getClassLoader().getResource("icons/128/black/ae.svg").getPath();


查看完整回答
反对 回复 2022-06-04
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

您正在混合两种不同的框架;classpath:与 Spring 有关,而SAXSVGDocumentFactory似乎与蜡染有关(https://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());


        }

    }

}

有关 Resource 的更多信息可以在这里找到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


查看完整回答
反对 回复 2022-06-04
  • 2 回答
  • 0 关注
  • 187 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号