1 回答
TA贡献1155条经验 获得超0个赞
我会建议使用的StreamingResponseBody,或者直接写OutputStream的HttpServletRequest。连同设置响应的内容类型和大小。
@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);
}
}
};
}
添加回答
举报