2 回答

TA贡献1834条经验 获得超8个赞
您可以尝试通过这种方式获取路径:
String path = SvgManagerApplication.class.getClassLoader().getResource("icons/128/black/ae.svg").getPath();

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
添加回答
举报