package com.wulaobo.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wulaobo.bean.Video;
import com.wulaobo.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Random;
@Controller
public class VideoController {
@Autowired
private VideoService videoService;
@PostMapping(value = "/videoUpload")
public String videoUpload(String title, MultipartFile file, HttpServletRequest request) throws IOException {
Video video = new Video();
String videoName = file.getOriginalFilename(); //获取上传后的文件名
// String newVideoName = this.getName(videoName); //根据上传的文件名重新生成一份新的文件名
// String path = request.getServletContext().getRealPath("video");
String path = "D:/upload/video/";
File videoPath = new File(path, videoName);
if (!videoPath.getParentFile().exists()) {
videoPath.getParentFile().mkdirs();
}
file.transferTo(videoPath);
video.setPath("/video/" + videoName);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
video.setUploadTime(timestamp);
video.setTitle(title);
video.setSize(this.getSize(videoPath));
video.setType(this.getFileExt(videoName));
boolean result = videoService.addVideo(video);
if (result) {
return "admin/uploadSuccess";
}
return "failed";
}
//管理员
@GetMapping(value = "/getVideoList")
public String getVideoList(@RequestParam(value = "pageNum", defaultValue = "1") Integer num, ModelMap model) {
PageHelper.startPage(num, 5);
PageHelper.orderBy("uploadTime desc");
List<Video> videoList = videoService.getVideoList();
PageInfo pageInfo = new PageInfo(videoList);
model.addAttribute("pageInfo", pageInfo);
return "admin/video/videoList";
}
//普通用户
@GetMapping(value = "/getVideoListByUser")
public String getVideoListByUser(@RequestParam(value = "pageNum", defaultValue = "1") Integer num, ModelMap model) {
PageHelper.startPage(num, 5);
PageHelper.orderBy("uploadTime desc");
List<Video> videoList = videoService.getVideoList();
PageInfo pageInfo = new PageInfo(videoList);
model.addAttribute("pageInfo", pageInfo);
return "frontPage/videoList";
}
//管理员点击播放按钮,开始播放视频
@GetMapping(value = "/videoPlayByIdAndAdmin")
public String videoPlayByIdAndAdmin(Integer id, ModelMap model) {
Video video = videoService.getVideoById(id);
model.addAttribute("title", video.getTitle());
model.addAttribute("path", video.getPath());
return "videoPlay";
}
@GetMapping(value = "/deleteVideoById")
public String deleteVideoById(Integer id, ModelMap model) {
Video video = videoService.getVideoById(id);
if (video != null) {
int result = videoService.deleteVideoById(id);
if (result > 0) {
File file = new File("D:/upload"+video.getPath());
if (file.exists()) {
file.delete();
return "forward:/getVideoList";
}
}
}
return "failed";
}
@GetMapping(value = "/downloadVideoById")
public void downloadVideoById(Integer id, HttpServletResponse response) throws IOException {
Video video = videoService.getVideoById(id);
String dict = "D:/upload/";
File file = new File(dict, video.getPath());
try {
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(video.getPath(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
FileInputStream fis = null;
OutputStream fos = response.getOutputStream();
if (file.exists()) {
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
fis.close();
fos.close();
}
/**
* 获取文件扩展名
*
* @return string
*/
private String getFileExt(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
}
/**
* 依据原始文件名生成新文件名
*
* @return
*/
private String getName(String fileName) {
Random random = new Random();
return "" + random.nextInt(10000) + System.currentTimeMillis();
}
/**
* 文件大小,返回kb.mb
*
* @return
*/
private String getSize(File file) {
String size = "";
long fileLength = file.length();
DecimalFormat df = new DecimalFormat("#.00");
if (fileLength < 1024) {
size = df.format((double) fileLength) + "BT";
} else if (fileLength < 1048576) {
size = df.format((double) fileLength / 1024) + "KB";
} else if (fileLength < 1073741824) {
size = df.format((double) fileLength / 1048576) + "MB";
} else {
size = df.format((double) fileLength / 1073741824) + "GB";
}
return size;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计:本人精品课程毕业设计的重构,使用springboot框架重写.zip (424个子文件)
config 269B
bootstrap.css 143KB
bootstrap.min.css 118KB
samples.css 64KB
editor_ie8.css 37KB
editor_iequirks.css 37KB
editor_ie.css 36KB
editor_gecko.css 36KB
editor.css 36KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
dialog_ie8.css 15KB
dialog_iequirks.css 14KB
dialog_ie.css 14KB
dialog.css 13KB
codemirror.css 8KB
balloonpanel.css 5KB
sample.css 5KB
balloonpanel.css 4KB
balloonpanel.css 4KB
easyimage.css 3KB
contents.css 3KB
default.css 2KB
page.css 2KB
outputxhtml.css 2KB
fontello.css 2KB
wsc.css 1KB
toolbar.css 1KB
wsc.css 1KB
tableselection.css 1KB
base.css 1KB
adminStyle.css 1KB
balloontoolbar.css 994B
balloontoolbar.css 914B
neo.css 815B
style4.css 698B
show-hint.css 662B
balloontoolbar.css 472B
style2.css 462B
dialog.css 396B
style3.css 395B
scayt.css 356B
style.css 248B
imagebase.css 185B
glyphicons-halflings-regular.eot 20KB
fontello.eot 5KB
FETCH_HEAD 132B
outputforflash.fla 84KB
spinner.gif 3KB
.gitignore 303B
HEAD 130B
HEAD 23B
datafiltering.html 46KB
index.html 15KB
inlineall.html 10KB
outputforflash.html 10KB
toolbar.html 9KB
magicline.html 8KB
fullpage.html 8KB
jquery.html 7KB
dialog.html 7KB
outputhtml.html 7KB
api.html 7KB
replacebyclass.html 7KB
replacebycode.html 7KB
xhtmlstyle.html 7KB
index.html 7KB
topicDetail.html 6KB
inlinebycode.html 6KB
adminHome.html 6KB
index.html 6KB
home.html 6KB
answerList.html 5KB
topicList.html 5KB
videoList.html 5KB
inlinetextarea.html 5KB
easyimage.html 5KB
divreplace.html 4KB
uilanguages.html 4KB
toUserUploadPage.html 4KB
enterkey.html 4KB
allUser.html 4KB
videoUpload.html 4KB
sourceList.html 4KB
newsList.html 4KB
sourceUpload.html 4KB
videoList.html 4KB
sourceList.html 4KB
topicList.html 4KB
scoreboardList.html 3KB
messageList.html 3KB
readonly.html 3KB
ajax.html 3KB
uicolor.html 2KB
editNews.html 2KB
update.html 2KB
tabindex.html 2KB
login.html 2KB
addNews.html 2KB
appendto.html 2KB
共 424 条
- 1
- 2
- 3
- 4
- 5
资源评论
九转成圣
- 粉丝: 5132
- 资源: 2961
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功