java web项目中访问src资源文件

在普通的web项目中我们可以这样去访问src文件

import org.springframework.util.ResourceUtils;

File file= ResourceUtils.getFile("classpath:test.txt");

但是在web项目打包成jar文件之后 我们就不能用这种方法去访问jar文件了

原因是 我们访问的时候是访问的绝对路径,而在经过打包之后jar里边的路径我们是访问不到的

这里我们只能用相对路径去访问当前的资源文件

classFilePath = "/public/......./filename";

ClassPathResource classPathResource = new ClassPathResource(classFilePath);

获取文件流:classPathResource .getInputStream();

利用这种方法我们就能成功的访问jar包里边的文件;

猜你喜欢

转载自www.cnblogs.com/sunke/p/9578588.html