处理非静态资源的简单解决方案:
@SpringBootApplicationpublic class DemoApplication { private final static File MP4_FILE = new File("/home/ego/bbb_sunflower_1080p_60fps_normal.mp4"); public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Controller final static class MyController { @Autowired private MyResourceHttpRequestHandler handler; // supports byte-range requests @GetMapping("/") public void home( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { request.setAttribute(MyResourceHttpRequestHandler.ATTR_FILE, MP4_FILE); handler.handleRequest(request, response); } // does not support byte-range requests @GetMapping(path = "/plain", produces = "video/mp4") public FileSystemResource plain() { return new FileSystemResource(MP4_FILE); } } @Component final static class MyResourceHttpRequestHandler extends ResourceHttpRequestHandler { private final static String ATTR_FILE = MyResourceHttpRequestHandler.class.getName() + ".file"; @Override protected Resource getResource(HttpServletRequest request) throws IOException { final File file = (File) request.getAttribute(ATTR_FILE); return new FileSystemResource(file); } }}
(受Spring Boots LogFileMvcEndpoint的启发,并且或多或少等于我稍后发现的Paul-Warrens(@ paul-warren)StoreByteRangeHttpRequestHandler)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)