Java servlet不会使用html5视频标记

时间:2016-08-23 07:32:04

标签: java html5 servlets video safari

html5视频代码无法从servlet加载mp4。

我的servlet代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Enumeration<String> enumn = request.getHeaderNames();
    while(enumn.hasMoreElements()){
        String hr = enumn.nextElement();
        System.out.println(hr + " - "+request.getHeader(hr));
    }       

    String fileName =  "test.mp4";
    String filePath =  "/Users/home/Downloads/test.mp4";

    String range = request.getHeader("range");
    File file = new File (filePath) ;
    long fileLength = file.length();        

    response.setContentType("video/mp4");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-control", "private");
    response.setHeader("Set-Cookie", "fileDownload=true; path=/");
    response.setDateHeader("Expires", 0);  
    response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName+"\"");  

    response.setHeader("Accept-Ranges", "bytes");
    response.setHeader("Etag", "W/\"9767057-1323779115364\"");
    response.setHeader("Connection", "keep-alive");

    response.setBufferSize((int)fileLength + 200);
    response.setContentLength((int)fileLength);
    response.setHeader("Content-Type", "video/mp4" );
    response.setHeader("Content-Length", String.valueOf( file.length()));

    BufferedInputStream  input = null ; 
    BufferedOutputStream output = null;
    try {

        input = new BufferedInputStream( new FileInputStream(file) );
        output = new BufferedOutputStream(response.getOutputStream());
        byte[] buffer = new byte[(int)fileLength];
        for (int length = 0 ; (length = input.read(buffer)) > 0 ;) {
            output.write(buffer, 0, length);
        }
        response.flushBuffer(); 
        System.out.println("buffer length : "+buffer.length);
        response.setHeader("Content-Range", "0-"+ Integer.valueOf(buffer.length-1));            
        System.out.println("=======================================================================");
        for(String hr : response.getHeaderNames()){
            System.out.println(hr + " - "+response.getHeader(hr));
        }

    }  catch (Exception e){
        e.printStackTrace();
    } finally {
        if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
        if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
    }
}

视频标记            

它在firefox和chrome中运行良好。但是safari无法加载视频。

0 个答案:

没有答案