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

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM