使用Spring MVC流式传输大型视频

时间:2015-03-16 09:12:52

标签: java spring-mvc video-streaming

使用Spring MVC传输大型视频文件时遇到问题。我的源代码如下。因此,它可以播放小于10 MB的视频文件,但是当我播放72 MB的视频时,服务器会抛出以下异常:“ClientAbortException:java.net.SocketException:软件导致连接中止:套接字写错误“

@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{

    }
}

感谢您解决此问题的任何帮助。谢谢!

0 个答案:

没有答案