Streaming video using Spring MVC

I ran into a problem while streaming large video files using Spring MVC. My source code is below. Thus, it can play a video file with a size of less than 10 MB, but when I play a video that is 72 MB, then the server throws the following exception: "ClientAbortException: java.net.SocketException: the software caused a connection interrupt: socket write error"

@RequestMapping(value = "/clip/{name}", method = RequestMethod.GET) @ResponseBody public void loadVideoFile(@PathVariable String name, HttpServletResponse response) { FileInputStream in = null; ServletOutputStream out = null; try { if (bundle == null) bundle = ResourceBundle.getBundle("course"); if (!bundle.containsKey("course.clip.Page" + name)) return; String filePath = bundle.getString("course.clip.Page" + name); int fileSize = (int) new File(filePath).length(); response.setContentLength(fileSize); response.setContentType("video/mp4"); in = new FileInputStream(filePath); out = response.getOutputStream(); int value = IOUtils.copy(in, out); System.out.println("File Size :: " + fileSize); System.out.println("Copied Bytes :: " + value); IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); response.setStatus(HttpServletResponse.SC_OK); } catch (java.io.FileNotFoundException e) { e.printStackTrace(); response.setStatus(HttpStatus.NOT_FOUND.value()); } catch (Exception e) { e.printStackTrace(); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); }finally{ } } 

I would be grateful for any help in solving this problem. Thanks!

+5
source share

Source: https://habr.com/ru/post/1215435/


All Articles