Spring Boot + Video 错误“视频播放因网络错误而中止”

所以我设计了一个应用程序,它有一个 reactjs 前端,从 Spring Boot 后端提供内容。我有一个提供视频的休息控制器。它在大约 10 分钟内运行良好,然后我的浏览器中出现“由于网络错误导致视频播放中止”。我在这里做错了吗?这应该在另一个线程或异步或其他线程上完成吗?我想这一切都是在春天完成的。


@RestController

@CrossOrigin

public class VideoDirectoryController {

@Autowired

private ConfigManager<LibraryConfig> librariesConf;


/**

 * 

 * @param libraryName

 * @param fileName

 * @return

 */

@GetMapping(value = "/getVideo", produces = "video/mp4")

public byte[] getVideo(@RequestParam(value = "library") String libraryName,

        @RequestParam(value = "fileName") String fileName) {

    Library lib = librariesConf.getConfig().getLibraries().get(libraryName.toLowerCase());

    Video video = lib.getVideoFiles().get(fileName);

    lib.getRecentlyViewed().add(video);

    librariesConf.getConfig().getLibraries().keySet().forEach((libr) -> System.out.println("LIBR: :" + libr));

    try (FileInputStream out = new FileInputStream(lib.getFileDirectory(fileName))) {

        return IOUtils.toByteArray(out);

    } catch (IOException e) {

        e.printStackTrace();

    }

    return null;

}

正如我所说,这可以正常工作约 10 分钟。如果我使用 :3000(前端)和直接点击获取视频的 URL(:8080/getVideo),它都会这样做


感谢任何帮助我以前从未在 Spring Boot 中使用多媒体。


倚天杖
浏览 399回答 1
1回答

白衣非少年

我会建议使用的StreamingResponseBody,或者直接写OutputStream的HttpServletRequest。连同设置响应的内容类型和大小。@GetMapping(value = "/getVideo", produces = "video/mp4")public void getVideo(@RequestParam(value = "library") String libraryName,&nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(value = "fileName") String fileName, HttpServletResponse response) {&nbsp; &nbsp; Library lib = librariesConf.getConfig().getLibraries().get(libraryName.toLowerCase());&nbsp; &nbsp; Video video = lib.getVideoFiles().get(fileName);&nbsp; &nbsp; lib.getRecentlyViewed().add(video);&nbsp; &nbsp; librariesConf.getConfig().getLibraries().keySet().forEach((libr) -> System.out.println("LIBR: :" + libr));&nbsp; &nbsp; try (FileInputStream out = new FileInputStream(lib.getFileDirectory(fileName))) {&nbsp; &nbsp; &nbsp; &nbsp; return StreamUtils.copy(out, response.getOutputStream());&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}或者同样使用 StreamingResponseBody@GetMapping(value = "/getVideo", produces = "video/mp4")public StreamingResponseBody getVideo(@RequestParam(value = "library") String libraryName,&nbsp; &nbsp; &nbsp; &nbsp; @RequestParam(value = "fileName") String fileName) {&nbsp; &nbsp; Library lib = librariesConf.getConfig().getLibraries().get(libraryName.toLowerCase());&nbsp; &nbsp; Video video = lib.getVideoFiles().get(fileName);&nbsp; &nbsp; lib.getRecentlyViewed().add(video);&nbsp; &nbsp; librariesConf.getConfig().getLibraries().keySet().forEach((libr) -> System.out.println("LIBR: :" + libr));&nbsp; &nbsp; return new StreamingResponseBody() {&nbsp; &nbsp; &nbsp; &nbsp; public void write(OutputStream out2) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try (FileInputStream out = new FileInputStream(lib.getFileDirectory(fileName))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StreamUtils.copy(out, out2);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python