springboot, jar 압축한 뒤에도 resource 밑 파일 읽기

bo-yoon·2021년 3월 7일

springboot

목록 보기
1/2

환경 : java 13, spring boot

jar 파일로 스프링 부트 애플리케이션을 압축한 뒤 로컬에서 돌렸을 때 문제 없던 소스가 jar 파일로 압축한 뒤 서버에 올리니 나타나지 않았다.

해결방법은 ClassPathResource로 경로를 설정하고, InputStreamReader를 사용해 읽는 것이다.

@GetMapping(value = "/sample", produces = "application/json") 
public String getSample() { 
			String str = ""; 
			String sb = ""; 
			ClassPathResource resource = null; 
			InputStreamReader reader = null; 
			
			
			try { 
			
					resource = new ClassPathResource("static/sample"); 
					reader = new InputStreamReader(resource.getInputStream(), "UTF-8"); 
					BufferedReader br = new BufferedReader(reader); 
					
					
					     while ((str = br.readLine()) != null) { 
					
									System.out.println(str); sb += str + "\n"; 
									
					     } 
				} catch (Exception e) { 
				
				
				e.printStackTrace();
        
        } 
        return sb; 
        
   }
   
profile
개발 로그 🍎 🍎 🍎

0개의 댓글