[SpringBoot] 파일 다운로드

2020. 7. 2. 22:35·IT/Spring

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
'IT/Spring' 카테고리의 다른 글
  • [SpringBoot] war 배포
  • [JPA] JPA Auditing
  • [JUnit] 테스트 기본 - 2
  • [JUnit] 테스트 기본 - 1
infe91
infe91
  • infe91
    돌고 돌아 다시 여기로
    infe91
  • 전체
    오늘
    어제
    • 분류 전체보기 (26)
      • IT (24)
        • Infra (15)
        • DB (0)
        • Spring (7)
        • Microservice (1)
        • IDE (1)
      • 공유 (1)
      • 일상 (1)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      cluster #RabbitMQ #disc #ram #cluster node type
      queryDSL #Spring Boot
      S3 #MinIO #미니오 #설치 #맥 #MinIO Browser
      비싼요금제
      미니오 #미니오 설치 #도커 #Docker #Minio #Minio docker
      7개월뒤
      mac #rabbitmq
      SpringBoot #file #파일 #다운로드 #파일 다운로드
      Centos7 #RabbitMQ #Managment UI
      Vagrant #Multi VM #Cpus #Memory #베그란트
      협동조합형
      젠킨스 #centos7 #허드슨 #Jenkins
      몽고 DB #도커 #도커 이미지 #도커 컨테이너
      war #톰캣 #스프링부트 #Tomcat
      모요
      kg모바일
      centos7 #systemd #minio
      위버하임
      펜타아너스
      헤센시티
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.0
    infe91
    [SpringBoot] 파일 다운로드
    상단으로

    티스토리툴바