web项目读取webapp下文件
window下
String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
String url = classpath.replaceAll("WEB-INF/classes/", "");//得到WebAPP目录
url = url + "/static/dist/img/zwtp1.jpg"
linux下
去掉 replaceFirst("/", “”);
String classpath = this.getClass().getResource("/").getPath();
String url = classpath.replaceAll("WEB-INF/classes/", "");//得到WebAPP目录
url = url + "/static/dist/img/zwtp1.jpg"
jar项目读取resources下文件
首先pom添加自己的路径
<resource>
<directory>src/main/resources/img</directory>
<filtering>false</filtering>
<includes>
<include>*</include>
</includes>
</resource>
window下
String imgPath = System.getProperty("user.dir") + "\\src\\main\\resources\\img\\zwtp1.jpg";
linux下
ClassPathResource classPathResource = new ClassPathResource("zwtp1.jpg");
InputStream in=classPathResource.getInputStream();//获取文件流
ClassPathResource 是freemarker的
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>