El proyecto web lee los archivos bajo la aplicación web.
debajo de la ventana
String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
String url = classpath.replaceAll("WEB-INF/classes/", "");//得到WebAPP目录
url = url + "/static/dist/img/zwtp1.jpg"
En Linux,
elimine replaceFirst ("/", "");
String classpath = this.getClass().getResource("/").getPath();
String url = classpath.replaceAll("WEB-INF/classes/", "");//得到WebAPP目录
url = url + "/static/dist/img/zwtp1.jpg"
El proyecto jar lee archivos bajo recursos
Primero agrega tu propio camino a pom
<resource>
<directory>src/main/resources/img</directory>
<filtering>false</filtering>
<includes>
<include>*</include>
</includes>
</resource>
debajo de la ventana
String imgPath = System.getProperty("user.dir") + "\\src\\main\\resources\\img\\zwtp1.jpg";
bajo linux
ClassPathResource classPathResource = new ClassPathResource("zwtp1.jpg");
InputStream in=classPathResource.getInputStream();//获取文件流
ClassPathResource es marcador libre
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>