Read the project file and get the path

The web project reads the files under the webapp

under the window

String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
String url = classpath.replaceAll("WEB-INF/classes/", "");//得到WebAPP目录 
url = url + "/static/dist/img/zwtp1.jpg"

Under linux
remove replaceFirst("/", "");

String classpath = this.getClass().getResource("/").getPath();
String url = classpath.replaceAll("WEB-INF/classes/", "");//得到WebAPP目录 
url = url + "/static/dist/img/zwtp1.jpg"

The jar project reads files under resources

First add your own path to pom

			<resource>
				<directory>src/main/resources/img</directory>
				<filtering>false</filtering>
				<includes>
					<include>*</include>
				</includes>
			</resource>

under the window

String imgPath = System.getProperty("user.dir") + "\\src\\main\\resources\\img\\zwtp1.jpg";

under linux

ClassPathResource classPathResource = new ClassPathResource("zwtp1.jpg");
InputStream in=classPathResource.getInputStream();//获取文件流

ClassPathResource is freemarker

		<dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>

Guess you like

Origin blog.csdn.net/weixin_43832604/article/details/111033946