//File Download
// import org.springframework.http.MediaType
// import org.springframework.core.io.Resource;
// import org.springframework.http.HttpHeaders;
// APPLICATION_OCTET_STREAM_VALUE => download할 수 있는 mime type
// User-Agent : HTTP Header Message 중 디바이스의 정보를 제공
// (웹 브라우저의 종류, 모바일, 데스크탑)
// IE에서 처리시 따로 처리가 필요함
@GetMapping(value = "/download",
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<Resource> downloadFile(@RequestHeader("User-Agent") String userAgent,
String fileName) {
// file 위치
String path = "/Users/andaegeun/Desktop/dditSpring/springProj/src/main/webapp/resources/upload/";
logger.info("download file : " + fileName);
Resource resource = new FileSystemResource(path + fileName);
// 파일이 없을때
if(!resource.exists()) {
//jsp에서 에러페이지로 넘기기
return new ResponseEntity<Resource>(HttpStatus.NOT_FOUND);
}
logger.info("resource : " + resource);
//file명 가져오기
String resourceName = resource.getFilename();
//file명이 한글이면
HttpHeaders headers = new HttpHeaders();
try {
String downloadName = null;
if(userAgent.contains("Trident")) {// Trident : IE version 11
logger.info("IE browser");
downloadName = URLEncoder.encode(resourceName, "utf-8").replaceAll("\\+", " ");
} else if(userAgent.contains("Edge")) {// Edge
logger.info("Edge browser");
downloadName = URLEncoder.encode(resourceName, "utf-8");
} else {
logger.info("Chrome browser");
downloadName = new String(resourceName.getBytes("utf-8"), "ISO-8859-1");
}
// 첫번째 파라미터 Content-disposition : 다운로드 시 저장되는 이름
// 두번째 파라미터 attachment;filename= : 한글일 경우 utf-8로 인코딩 해주기 위해서 잡아줌
headers.add("Content-disposition", "attachment;filename=" + downloadName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// ressource : 첨부파일 객체
// headers : 파일명 처리 정보
return new ResponseEntity<Resource>(resource, headers, HttpStatus.OK);
}