gpt4 book ai didi

java - Spring Boot + 视频错误 "video playback aborted due to a network error"

转载 作者:行者123 更新时间:2023-11-30 06:00:22 24 4
gpt4 key购买 nike

所以我设计了一个应用程序,它有一个reactjs前端,从spring boot后端提供内容。我有一个提供视频的休息 Controller 。它运行得非常好,持续了大约 10 分钟,然后我在浏览器中收到“视频播放因网络错误而中止”的信息。我在这里做错了什么吗?这应该在另一个线程或异步或其他线程上完成吗?我想这一切都是由 Spring 完成的。

@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 中使用过多媒体。

最佳答案

我建议使用 StreamingResponseBody或者直接写入 HttpServletRequestOutputStream。以及设置响应的内容类型和大小。

@GetMapping(value = "/getVideo", produces = "video/mp4")
public void getVideo(@RequestParam(value = "library") String libraryName,
@RequestParam(value = "fileName") String fileName, HttpServletResponse response) {
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 StreamUtils.copy(out, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}

或者使用 StreamingResponseBody 进行相同的操作

@GetMapping(value = "/getVideo", produces = "video/mp4")
public StreamingResponseBody 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));
return new StreamingResponseBody() {
public void write(OutputStream out2) throws IOException {
try (FileInputStream out = new FileInputStream(lib.getFileDirectory(fileName))) {
StreamUtils.copy(out, out2);
}
}
};
}

关于java - Spring Boot + 视频错误 "video playback aborted due to a network error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52365308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com