반응형

ClassPathResource resource = new ClassPathResource(resourcePath);

File file = resource.getFile();

 

위와 같이 코드를 작성해서 로컬에서 실행했을 때에 아무 문제 없이 동작을 하던 것이 war File 배포한 후에 동작을 시켰을 때에 에러가 발생을 했다.

 

class path resource [img/header.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/xxxxadmin/lib/xxxxxxxxx.war!/WEB-INF/classes!/img/header.jpg

java.io.FileNotFoundException: class path resource [img/header.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/xxxxadmin/lib/xxxxxxxxx.war!/WEB-INF/classes!/img/header.jpg

at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215)

at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)

 

분명히 존재하는데 찾지를 못하는 거지???

 

https://sonegy.wordpress.com/2015/07/23/spring-boot-executable-jar%EC%97%90%EC%84%9C-file-resource-%EC%B2%98%EB%A6%AC/

 

문제는 소스코드에 있는 resource.getFile() 메소드 때문이었다.

 

war 파일이나 IDE로 application run as로 실행하였다면 실제 resource 파일인 file:// 프로토콜을 쓰기 때문에 File객체를 생성해 줄 수 있지만, executable jar로 실행 했다면 FileNotFoundException이 발생 하게 됩니다.


그래서 아래와 같이 변경해주면 해결이 된다.


InputStream inputStream = classPathResource.getInputStream();

File tempImage = File.createTempFile("temp", ".jpg");

try {

    FileUtils.copyInputStreamToFile(inputStream, tempImage );

} finally {

    IOUtils.closeQuietly(inputStream);

}


728x90
반응형

+ Recent posts