Video Using HTML 5 and servlet

后端 未结 4 1277
悲&欢浪女
悲&欢浪女 2021-02-09 18:02

Below given code is for video streaming. This is fine with IE9 and firefox but it is not fine with Chrome and Mac Safari.

import java.io.*;
import javax.servle         


        
4条回答
  •  滥情空心
    2021-02-09 18:12

    Honestly, this approach is absolutely not right.

    • You are sniffing the user agent in the server side and depending the business job on it. This is in all cases a bad idea. If all you want is to specify a different file depending on the user agent, then rather do it in the HTML side, with help of JavaScript or CSS. Both client side languages are able to identify the real browser without the need to sniff the user agent string (which is namely spoofable).

    • You are not responding correctly on Range requests. You're sending the complete file back instead of the requested Range. Firefox and IE do not use range requests and that's why it "works". Chrome and Safari use range requests.

    This should be possible without sniffing the user agent and properly responding to Range requests by RandomAccessFile instead of File and byte[]. It's only pretty a lot of code to take all HTTP specification requirements into account, so here's just a link where you can find a concrete example of such a servlet: FileServlet supporting resume and caching.

    However, much better is to delegate the job to the servletcontainer's default servlet. If it's for example Tomcat, then all you need to do is to add the following line to /conf/server.xml:

    
    

    This way the desired media files are just available by http://localhost:8080/media/final.ogg and http://localhost:8080/media/final.mp4 without the need to homegrow a servlet.

提交回复
热议问题