大家好,又见面了,我是你们的朋友全栈君。
java web实现video播放
1.前端的实现
- 通过getQueryVariable获取url的参数(vid )
function getQueryVariable(variable){
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++){
var pair = vars[i].split("=");
if(pair[0] == variable){
return pair[1];}
}
return(false);
}
var data={
vid:getQueryVariable("vid") };
- 在single播放页加入播放控件
<!--播放-->
<video id ="video_iframe" src ="#"controls = "controls"style="..."class="video- grid-single-page-agileits"width="750" height="400"></video>
- 通过ajax上传获取的vid值和播放控件实现服务器的数据交换,实现网页布局更新
$.ajax({
url:"/getvideobyid",
type:"post",
data:data,
dataType:"json",
success:function (result) {
// 取id="video_iframe",调用后台反过来的videopath路径,实现播放功能
$("#video_iframe").attr("src","http://192.168.5.103/"+result.videopath);
$("#myname").html(result.name); }
});
- 在index页面实现跳转事件
<a href="single.html?vid=${movie.vid}" class="hvr-shutter-out-horizontal">
2.后台实现对数据的查询等交互功能
- 接收网页传过来的vid值
String vid = req.getParameter("vid");//1`
- VideoDao实现:通过vid查找并返回数据库中的movie对象
public class VideoDao extends BaseDao {
public Movie queryVideo(String vid){
String sql = "select * from video where vid = ?";
List<Map<String,Object>> maps = super.queryAnything(sql,vid);
String name = String.valueOf(maps.get(0).get("name"));
String author = String.valueOf(maps.get(0).get("author"));
String imagepath = String.valueOf(maps.get(0).get("imagepath"));
String videopath =String.valueOf(maps.get(0).get("videopath"));
Timestamp time =Timestamp.valueOf(maps.get(0).get("createtime").toString());
String type= String.valueOf(maps.get(0).get("type"));
Movie movie = new Movie();
movie.setCreatetime(time);
movie.setVideopath(videopath);
movie.setImagepath(imagepath);
movie.setType(type);
movie.setAuthor(author);
movie.setName(name);
return movie;
}
}
- 在VideoService中定义
public interface VideoService {
public Movie queryMovieById(String vid);
}
- 在VideoServiceImpl定义
//queryMovieById实现通过vid查找并返回相对应的movie对象
public class VideoServiceImpl implements VideoService{
@Override public Movie queryMovieById(String vid) {
VideoDao videoDao = new VideoDao();
Movie movie = videoDao.queryVideo(vid);
return movie;
}
}
- VideoPlayServlet实现返回查询结果(movie)给网页
public class VideoPlayServelet extends HttpServlet {
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
String vid = req.getParameter("vid");//1
VideoService videoService = new VideoServiceImpl();
Movie movie = videoService.queryMovieById(vid);
//ajax json 数据在网络上传输都是字符串
Gson gson = new Gson();
String result = gson.toJson(movie);
//将数据返给客户
resp.getWriter().write(result);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/156372.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...