shikeying
2022-09-27 26f5dd8ef80e5671cda8fc0e6c0d0298c4e678ff
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.iplatform.recvideo.controller;
 
import com.iplatform.model.po.Rc_video_batch;
import com.iplatform.model.po.Rc_video_t2;
import com.iplatform.recvideo.config.VideoSimilarProperties;
import com.iplatform.recvideo.service.VideoShowServiceImpl;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.List;
 
@Controller
@RequestMapping("/video_ui")
public class VideoResultController {
 
    private VideoShowServiceImpl videoShowService;
    private VideoSimilarProperties videoSimilarProperties;
 
    @Autowired
    public VideoResultController(VideoShowServiceImpl videoShowService, VideoSimilarProperties videoSimilarProperties){
        this.videoShowService = videoShowService;
        this.videoSimilarProperties = videoSimilarProperties;
    }
 
    @GetMapping("/index")
    public String index(Model model, String id) {
 
        GenericPager<Rc_video_batch> pager = this.videoShowService.queryPageVideoBatchList();
        List<Rc_video_batch> data = pager.getDatas();
        model.addAttribute("src_list", data);
        model.addAttribute("video_folder", this.videoSimilarProperties.getDataFolder());
 
        if(StringUtils.isNotEmpty(id)){
            List<Rc_video_t2> list = this.videoShowService.querySimilarVideoList(id);
            model.addAttribute("similar_list", list);
            model.addAttribute("select_id", id);
        } else {
            model.addAttribute("select_id", null);
        }
        return "video/index";
    }
 
    /**
     * 本地视频转视频流流
     * @param response
     * @return
     */
    @RequestMapping("/get_video")
    @ResponseBody
    public void getVideo(
//            HttpServletRequest request,
            HttpServletResponse response, String id) {
        if(StringUtils.isEmpty(id)){
            System.out.println("视频ID未提供");
            return;
        }
        //视频资源存储信息
        response.reset();
        //获取从那个字节开始读取文件
//        String rangeString = request.getHeader("Range");
        String rangeString = null;
 
        Rc_video_batch videoInfo = this.videoShowService.queryOneVideoInfo(id);
        if(videoInfo == null){
            System.out.println("Rc_video_batch未查找到对象: " + id);
            return;
        }
        String videoPath = videoInfo.getSrc_video_path();
        if(this.videoSimilarProperties.isTestMode()){
            // 测试模式,需要替换文件根路径为本机windows路径
            StringBuilder windowPath = new StringBuilder(this.videoSimilarProperties.getDataFolder());
            windowPath.append(videoInfo.getBatch_id());
            windowPath.append("/").append(id).append(".mp4");
            videoPath = windowPath.toString();
        }
 
        try {
            //获取响应的输出流
            OutputStream outputStream = response.getOutputStream();
//            File file = new File("c:\\video.mp4");
            File file = new File(videoPath);
            if(file.exists()){
                RandomAccessFile targetFile = new RandomAccessFile(file, "r");
                long fileLength = targetFile.length();
                //播放
                if(rangeString != null){
                    long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
                    //设置内容类型
                    response.setHeader("Content-Type", "video/mp4");
                    //设置此次相应返回的数据长度
                    response.setHeader("Content-Length", String.valueOf(fileLength - range));
                    //设置此次相应返回的数据范围
                    response.setHeader("Content-Range", "bytes "+range+"-"+(fileLength-1)+"/"+fileLength);
                    //返回码需要为206,而不是200
                    response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                    //设定文件读取开始位置(以字节为单位)
//                    targetFile.seek(range);
                }else {//下载
                    //设置响应头,把文件名字设置好
                    response.setHeader("Content-Disposition", "attachment; filename=video.mp4" );
                    //设置文件长度
                    response.setHeader("Content-Length", String.valueOf(fileLength));
                    //解决编码问题
                    response.setHeader("Content-Type","application/octet-stream");
                }
 
                byte[] cache = new byte[1024 * 300];
                int flag;
                while ((flag = targetFile.read(cache))!=-1){
                    outputStream.write(cache, 0, flag);
                }
            }else {
                String message = "file: not exists";
                //解决编码问题
                response.setHeader("Content-Type","application/json");
                outputStream.write(message.getBytes(StandardCharsets.UTF_8));
            }
 
            outputStream.flush();
            outputStream.close();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}