postman 测试视频流接口

目的: 利用postman测试视频流接口 即 通过接口下载视频文件

框架:springboot

步骤
1.  编写接口
@Controller
public class TestController {
    @RequestMapping(value = "/downloadVedio",method = RequestMethod.POST)
    @ResponseBody
    public void show(HttpServletRequest request, HttpServletResponse response) {
        File file = new File("C:\\Users\\hspcadmin\\Desktop\\temp\\ff.mp4");// 视频路径
        if(!file.exists()){
            throw new RuntimeException("视频文件不存在!");
        }
        byte[] fba = getFileByteArr(file);

        OutputStream sos = null;
        try {
            response.setHeader("Content-Type", "video/mp4");
            sos = response.getOutputStream();
            sos.write(fba);
            sos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private byte[] getFileByteArr (File file){
        byte[] buffer = null;
        try{
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return buffer;
    }
}    

 2. 配置postman && 测试结果

 

 完整demo路径: https://github.com/green241/postmanDemo.git

 


 

posted @ 2021-02-26 17:58  lvlin241  阅读(2566)  评论(0编辑  收藏  举报