ResourceUtils.getURL("classpath:static").getPath()
spring boot 打包获取静态文件:
public class ClassPathResouceReader {
private final String path;
private String content;
private File file;
public ClassPathResouceReader(String path) {
this.path = path;
}
public File getFile(String prefix, String suffix) {
if (file == null) {
InputStream inputStream = null;
try {
ClassPathResource classPathResource = new ClassPathResource(path);
inputStream =classPathResource.getInputStream();
file = File.createTempFile(prefix, suffix);
FileUtils.copyInputStreamToFile(inputStream, file);
} catch (Exception e) {
log.error(JSON.toJSONString(e.getStackTrace()));
} finally {
IOUtils.closeQuietly(inputStream);
}
}
return file;
}
public String getContent(){
if (content == null) {
BufferedReader reader = null;
try {
ClassPathResource classPathResource = new ClassPathResource(path);
reader = new BufferedReader(new InputStreamReader(classPathResource.getInputStream()));
content = reader.lines().collect(Collectors.joining("\n"));
} catch (Exception e) {
log.error(JSON.toJSONString(e.getStackTrace()));
}finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
log.error(JSON.toJSONString(e.getStackTrace()));
}
}
}
}
return content;
}
}
本地运行时获取:
ResourceUtils.getFile("classpath:static/js/hmac-sha1.js");