[DEV] 기록

[Spring Batch] Input resource must exist (reader is in 'strict' mode)

꾸준함. 2023. 11. 13. 02:01

개요

Spring Batch를 실행하는데 FlatFileItemReader가 JobParameter를 통해 전달받은 파일을 open() 메서드를 통해 열 때 아래와 같은 오류가 발생했습니다.

 

 Input resource must exist (reader is in 'strict' mode)

 

 

해결 방법

resource 파라미터에 new PathResource("절대 경로")로 전달하면 위 문제가 해결이 됩니다.

다만, 이 방법은 절대 경로를 전달받을 경우에만 해결이 가능한 방법입니다.

 

상대경로를 전달 받을 경우 절대 경로를 구하는 메서드를 아래와 같이 구현해야 합니다.


public String getReceivedFilePath(String fileName) {
File directory = new File(file.location);
if (directory.isDirectory()) {
File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith("extension.type"));
for (File file : files) {
if (FilenameUtils.getBaseName(file.getName()).startsWith(FilenameUtils.getBaseName(fileName))) {
return file.getAbsolutePath();
}
}
}
return directory.getName();
}
view raw .java hosted with ❤ by GitHub

 

 

그리고 new PathResource(getReceivedFilePath("전달 받은 상대 경로"))를 resource 파라미터에 전달하면 됩니다.

 

출처

https://stackoverflow.com/questions/46403159/spring-batch-input-resource-must-exist-reader-is-in-strict-mode-error

 

Spring batch Input resource must exist (reader is in 'strict' mode) error

I use Spring Batch for parse csv file. It works great, when file in resource directory, but doesn't work from another place. I get suck error Caused by: java.lang.IllegalStateException: Input reso...

stackoverflow.com

 

반응형