2 回答
TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以嘗試通過這種方式獲取路徑:
String path = SvgManagerApplication.class.getClassLoader().getResource("icons/128/black/ae.svg").getPath();TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
您正在混合兩種不同的框架;classpath:與 Spring 有關(guān),而SAXSVGDocumentFactory似乎與蠟染有關(guān)(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());
}
}
}
有關(guān) Resource 的更多信息可以在這里找到https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/Resource.html,而有關(guān) ClassPathResource 的更多信息可以找到這里https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html
添加回答
舉報(bào)
