ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스프링부트_파일 다운로드
    Spring Boot 2019. 8. 29. 22:12

    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/jeong/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);
    			}
    			
    		};
    	}
    
    }

     

     

    https://gitlab.com/kabby-play-code/msa/toy-project-1/msa-video/back-end

    'Spring Boot' 카테고리의 다른 글

    스프링 부트_mybatis(마이바티스)  (0) 2019.08.29
    스프링부트_Swagger2  (0) 2019.08.29
    스프링부트_톰캣 (war) 배포  (0) 2019.08.29
Designed by Tistory.