@RunWith(SpringRunner.class)
@SpringBootTest
public class RestTemplateTest {
@Autowired
RestTemplateBuilder restTemplateBuilder;
@Test
public void RestTemplate를_이용하여_이미지_최신화() throws URISyntaxException {
// 체크할 이미지 파일의 URL주소
String imgSrc = "http://steamcdn-a.akamaihd.net/steam/apps/359550/capsule_sm_120.jpg";
// 스프링부트의 빌더를 이용하여 RestTemplate 생성
RestTemplate restTemplate = restTemplateBuilder.build();
// 헤더만을 가져오도록 제공되는 메서드 사용
HttpHeaders httpHeaders = restTemplate.headForHeaders(new URI(imgSrc));
String lastModified = httpHeaders.get(HttpHeaders.LAST_MODIFIED).get(0);
// 응답의 헤더는 GMT 시간
System.out.println("lastModified : " + lastModified); // Fri, 21 Feb 2020 19:40:42 GMT
// 응답의 시간에 맞는 포맷터를 지정하여 ZonedDateTime(Asia/Seoul) 객체 생성
ZonedDateTime lastModifiedZDT = ZonedDateTime.parse(lastModified, DateTimeFormatter.RFC_1123_DATE_TIME)
.withZoneSameInstant(ZoneId.of("Asia/Seoul"));
System.out.println("lastModifiedZDT : " + lastModifiedZDT); // 2020-02-22T04:40:42+09:00[Asia/Seoul]
// ZonedDateTime -> LocalDateTime
LocalDateTime lastModifiedLDT = lastModifiedZDT.toLocalDateTime();
System.out.println("lastModifiedLDT : " + lastModifiedLDT); // 2020-02-22T04:40:42
// 기준일시를 생성하여 체크!
LocalDateTime stdLDT = LocalDateTime.now().minusDays(1); // 기준일시(하루전) 생성
if(lastModifiedLDT.isAfter(stdLDT)){ // 기준일시 이후에 변경이 일어나면
System.out.println("다운로드(최신화) 필요함");
}else{
System.out.println("기존과 같음");
}
}
}