@GetMapping(value = "", produces = "application/json;charset=utf-8")
public ResponseEntity getFile(
@RequestParam(value = "fileName", required = false) String fileName) throws IOException {
ResponseEntity respEntity = null;
File result = new File("YOUR_PATH" + fileName);
if (result.exists()) {
InputStream inputStream = new FileInputStream("YOUR_PATH" + fileName);
String type = URLConnection.guessContentTypeFromStream(inputStream);
byte[] out = org.apache.commons.io.IOUtils.toByteArray(inputStream);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("content-disposition", "attachment; filename=" + fileName);
responseHeaders.add("Content-Type", type);
respEntity = new ResponseEntity(out, responseHeaders, HttpStatus.OK);
} else {
respEntity = new ResponseEntity("File Not Found", HttpStatus.OK);
}
return respEntity;
}
ref : https://stackoverflow.com/questions/32641231/return-file-in-spring-mvc-rest
'Spring Boot' 카테고리의 다른 글
Spring Actuator와 Swagger가 서로 충돌할때 (0) | 2023.09.19 |
---|---|
[Swagger] swagger-ui.html 접속 url 변경하기 (리다이렉트) (0) | 2023.08.09 |
[Spring] 타임리프에서 Spring Service 호출하기 (0) | 2023.08.08 |
[Spring] Lombok을 이용한 간단한 의존성 생성자 주입 (0) | 2023.08.01 |
[Spring] List를 Page로 변환하기 (0) | 2023.07.12 |