본문 바로가기
프로그래밍

SpringBoot 웹서버 Jar 에서 resource 경로의 파일을 못 찾는 오류

by 대도루피 2023. 5. 18.
반응형

SpringBoot로 개발을 진행중입니다.

 

예전이야 Tomcat을 썼지만, 지금은 MSA로서 jar 단독으로 서버를 실행시키고 있습니다.

예전 Tomcat을 사용했을때 문제가 없던 코드인데, gradle에서 bootJar 로 패키징했을 경우 리소스 경로를 찾는데 문제가 있습니다.

File Not Found 오류가 발생하네요.

 

기존에는 대강 이런식으로 처리했었고요, 

URL res = getClass().getResource( UPDATE_SQL_PATH);
URI uri = res.toURI();
FileSystem fileSystem = FileSystems.newFileSystem( uri, Collections.<String, Object>emptyMap());
Path path = fileSystem.getPath(UPDATE_SQL_PATH);

Stream<Path> walk = Files.walk( path, 1);
for ( Iterator<Path> it = walk.iterator(); it.hasNext();){
    String path = UPDATE_SQL_PATH + "/" + it.getFileName();
	executeSqlFile(path)

}

private void executeSqlFile(String path) {
	InputStream is = getClass().getResourceAsStream( fileUrl);
}

 

이렇게 간단하게 수정가능합니다.

executeSqlFile에서 하는 일이 파일 내용 읽어서 그대로 sql 쿼리를 실행하는 것 입니다.

그래서 inputStream으로 읽어서 처리하는거죠.

String cpth = "classpath*:" + UPDATE_SQL_PATH + "/*.sql";
Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader()).getResources(cpth);
for (Resource r : resources) {
	executeSqlFile(r);
}

private void executeSqlFile(Resource r) {
	InputStream is = r.getInputStream()
    // is를 읽어 처리
}

결과적으로 is로만 읽으면 된다..

그리고 StringBoot에서 간단하게 쓸 수 있는 util 을 제공해준다... 로 보면 될듯 합니다.

반응형