目的: 利用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