Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 퀵소트
- upgrade
- git
- update
- eslint
- expire_logs_days
- nodejs
- regex
- Effective Java 3/e
- REACT
- Regular expression
- npm
- REACTJS
- spring
- current_date
- Effective Java
- spring cloud
- Webpack
- Chunk
- mysql 5.5
- log_bin
- log4j2
- java
- Express
- Node
- JavaScript
- 정규표현식
- migration
- MySQL
- Spring Batch
Archives
- Today
- Total
내 세상
[Spring] ResponseEntity / File Download 구현 본문
728x90
private HttpHeaders makeHttpHeaders(String filename) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filename + "\";");
headers.set(HttpHeaders.TRANSFER_ENCODING, "binary");
return headers;
}
private ResponseEntity<?> downloadFile(HttpServletRequest request, Map<String, Object> map, String originUrl)
throws Exception {
String prefix = constants.getPrefix();
String subPath = getArray(partPathArr, 0, 2);
File[] fileList = new File(prefix + subPath).listFiles();
// 코드 중간 생략
if (fileName.isEmpty()) {
return ResponseEntity.notFound().build();
}
String path = prefix + subPath + File.separator + fileName;
InputStreamResource resource = new InputStreamResource(new FileInputStream(path));
return ResponseEntity.ok().headers(makeHttpHeaders(fileName)).contentLength(new File(fileName).length())
.contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource);
}
InputStreamResource가 다른 ByteArrayResource, Resource, FileSystemResource에 비해서 대용량을 사용할 수 있다고함.
추후 비교 예정.
728x90
'Technical > Spring' 카테고리의 다른 글
[Spring Webflux][R2DBC] The server timezone is <????> that's unknown, trying to use system default timezone 조지기 (0) | 2025.02.26 |
---|---|
[Spring] @PropertySource Default 설정 (0) | 2022.09.14 |
[Spring] URL Shorten 서비스 개발 (0) | 2021.06.23 |
[Spring] 하위 디렉토리/ 폴더 한번에 삭제 하기 (0) | 2020.09.15 |
[Spring] 전역 변수 사용하기 (0) | 2020.08.28 |