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