无法理解 URL 以连接读取 svg 文件
Unable to make sense of URL for connection reading a svg file
我有一个 Spring Boot 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());
}
}
}
你可以试试这样获取路径:
String path = SvgManagerApplication.class.getClassLoader().getResource("icons/128/black/ae.svg").getPath();
您正在混合使用两种不同的框架; 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());
}
}
}
可以在此处找到有关资源的更多信息https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/Resource.html, while more information about ClassPathResource can be found here https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html
我有一个 Spring Boot 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());
}
}
}
你可以试试这样获取路径:
String path = SvgManagerApplication.class.getClassLoader().getResource("icons/128/black/ae.svg").getPath();
您正在混合使用两种不同的框架; 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());
}
}
}
可以在此处找到有关资源的更多信息https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/Resource.html, while more information about ClassPathResource can be found here https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html