package cn.itcast.czjf.web.servlets;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import com.google.gson.Gson;
import cn.itcast.czjf.dao.VideoDao;
import cn.itcast.czjf.domain.TachMaterial;
import cn.itcast.czjf.domain.Video;
import cn.itcast.czjf.service.VideoService;
import cn.itcast.czjf.utils.DownLoadUtils;
import cn.itcast.czjf.utils.PageModel;
import cn.itcast.czjf.utils.UploadUtils;
import cn.itcast.czjf.web.base.BaseServlet;
public class VideoServlet extends BaseServlet {
private static final long serialVersionUID = 1L;
public String getVedios(HttpServletRequest request, HttpServletResponse response) throws Exception{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
VideoService videoService=new VideoService();
PrintWriter writer = response.getWriter();
List<Video> ulist=videoService.getVideos();
Gson gson=new Gson();
String json = gson.toJson(ulist);
writer.println(json);
return null;
}
/**
* 查询最新的五个视频
*/
public String findNewVideo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
VideoService videoService=new VideoService();
List<Video> list=videoService.findNewVideo();
request.setAttribute("list", list);
return "/site/vedio/vedioPrev.jsp";
}
/**
* 前台分页查询更多视频资源
*/
public String findAllVideoWithPage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.获取当前页参数
int num = Integer.parseInt(request.getParameter("num"));
//2.调用业务层。返回pagemodel。其以下有解释原因
VideoService videoService=new VideoService();
//.由于当前的功能是分页查询,除了查询当前页中的视频,还需要获取到分页参数
PageModel pm=null;
try {
pm = videoService.findAllVideoWithPage(num);
} catch (SQLException e) {
e.printStackTrace();
}
//如果仅仅是当前的视频,用一个集合就搞定,但是分页参数比较复杂,所以使用PageModel工具类携带这类数据
//3.将pagemodel放到请求域。然后转发到指定的页面findVideoInfoById
request.setAttribute("page", pm);
request.setAttribute("num", num);
return "/site/vedio/vedioAll.jsp";
}
/**
* 通过id 查询视频详细信息
*/
public String findVideoInfoById(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
VideoService videoService=new VideoService();
Video video1=null;
try {
video1 = videoService.findVideoInfoById(id);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("video1", video1);
return "/site/vedio/vedioDetail.jsp";
}
/**
* 点击播放视频playVideoById
*/
public String playVideoById(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
VideoService videoService=new VideoService();
Video video=null;
try {
video=videoService.findVideoInfoById(id);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("video",video);
return "/play/playVedio.jsp";
}
/**
* 下载视频模块。根据视频id
* downLoadVideo;数据不需要转发到任何页面
*/
public String downloadVideo(HttpServletRequest request, HttpServletResponse response)
throws IOException, SQLException {
String id = request.getParameter("id");
System.out.println(id);
VideoService videoService=new VideoService();
Video video = videoService.findVideoInfoById(id);
//获取项目下的upload绝对路径
String realPath = getServletContext().getRealPath("/upload/");
//实例化File代表,代表下载的视频
File file=new File(realPath,video.getVedioAttachment());
//通过response对象设置一个消息头==调用工具类更加好
DownLoadUtils.setConentType(request, video.getVedioAttachment(), response);
//通过File获取一个输入流
InputStream is=new FileInputStream(file);
//通过response获取输出流
OutputStream os = response.getOutputStream();
//将输入流中的数据刷出到输出流
IOUtils.copy(is, os);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
return null;
}
public String downloadVideoByTeacher(HttpServletRequest request, HttpServletResponse response)
throws IOException, SQLException {
String id = request.getParameter("id");
System.out.println(id);
VideoService videoService=new VideoService();
Video video = videoService.findVideoInfoByIdWithTeacher(id);
//获取项目下的upload绝对路径
String realPath = getServletContext().getRealPath("/upload/");
//实例化File代表,代表下载的视频
File file=new File(realPath,video.getVedioAttachment());
//通过response对象设置一个消息头==调用工具类更加好
DownLoadUtils.setConentType(request, video.getVedioAttachment(), response);
//通过File获取一个输入流
InputStream is=new FileInputStream(file);
//通过response获取输出流
OutputStream os = response.getOutputStream();
//将输入流中的数据刷出到输出流
IOUtils.copy(is, os);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
return null;
}
/**
* 老师分页查询视频信息
* teacherFindAllVideosWithPage
* /czjf_system/WebRoot/atea/vedio/vedioMana.jsp
*/
public String findAllVideosWithPageByTeacher(HttpServletRequest request, HttpServletResponse response)
throws IOException, SQLException {
int currentPageNum=Integer.parseInt(request.getParameter("num"));
VideoService videoService=new VideoService();
PageModel pm=videoService.findAllVideosWithPageByTeacher(currentPageNum);
request.setAttribute("page", pm);
request.setAttribute("num", currentPageNum);
return "/atea/vedio/vedioMana.jsp";
}
/**
* 根据id删除视频
* deleteVideoById
*/
public String deleteVideoById(HttpServletRequest request, HttpServletResponse response)
throws IOException, SQLException {
String vedioId = request.getParameter("vedioId");
VideoService videoService=new VideoService();
videoService.deleteVideoById(vedioId);
response.sendRedirect("/czjf_system/VideoServlet?method=findAllVideosWithPageByTeacher&num=1");
return null;
}
/**
* 添加视频UI(页面)
* addVideoUI
*/
public String addVideoUI(HttpServletRequest request, HttpServletResponse response)
throws IOException, SQLException {
return "/atea/vedio/vedioAdd.jsp";
}
/**
* 老师上传视频
* addVideoByTeacher
*/
public String addVideoByTeacher(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// InputStream is=request.getInputStream();//获取前台传来的视频信息;用于测试,后台能否正常获取值
// int i=is.read();
// while(i!=-1){
// System.out.print((char)i);
// i=is.read();
// }
Map<String, Object> map=new HashMap<String, Object>();//携带表单名称以及表单参数
Video video=new Video();//携带video属性
//1.创