VideoRestController.java
@GetMapping(value = "/{videoId}/download", produces = MediaType.APPLICATION_JSON_VALUE)
public StreamingResponseBody findVideoFileDownload(@PathVariable(name="videoId") String videoId,HttpServletResponse response) throws FileNotFoundException {
// TODO Auto-generated method stub
return this.videoServiceImpl.findVideoFileDownload(videoId, response);
}
VideoService.java
public interface VideoService {
public StreamingResponseBody findVideoFileDownload(String videoId, HttpServletResponse response) throws FileNotFoundException;
}
VideoLogic.java
public class VideoLogic implements VideoService {
private VideoRepositoryStore videoRepository;
public VideoLogic(VideoRepositoryStore videoRepository) {
this.videoRepository = videoRepository;
}
@SuppressWarnings("resource")
@Override
public StreamingResponseBody findVideoFileDownload(String videoId, HttpServletResponse response) throws FileNotFoundException {
/*
* 다운 받을 영상 경로 예 : /Users/kabby/Desktop/video/tea.mp4
*
* 확장자 : mp4
*
*/
Video video=this.videoRepository.retrieveVideoById(videoId);
response.setContentType("video/mp4");
response.setHeader("Content-Disposition", "attachment;filename=" + video.getVideoName() + "." + video.getVideoExtention());
File targetFile = new File(video.getVideoPath());
InputStream targetStream = new DataInputStream(new FileInputStream(targetFile));
return outputStream -> {
int nRead;
byte[] data = new byte[4069];
while ((nRead = targetStream.read(data, 0, data.length)) != -1) {
outputStream.write(data, 0, nRead);
}
};
}
}
'IT > Spring' 카테고리의 다른 글
[SpringBoot] war 배포 (0) | 2020.07.02 |
---|---|
[JPA] JPA Auditing (0) | 2020.02.16 |
[JUnit] 테스트 기본 - 2 (0) | 2020.02.13 |
[JUnit] 테스트 기본 - 1 (0) | 2020.02.13 |
[JPA] 트랜잭션과 락 (0) | 2020.02.12 |