import cn.com.connext.commenservice.constant.FileType;
import cn.com.connext.commenservice.entity.common.LYResultVO;
import cn.com.connext.commenservice.entity.ecourse.EcourseVideoEntity;
import cn.com.connext.commenservice.utils.FileTypeJudge;
import cn.com.connext.commenservice.utils.InitaZureConnection;
import cn.com.connext.loreal_course.service.EcourseVideoService;
import cn.com.connext.loreal_course.utils.MultipartFileToFile;
import cn.com.connext.loreal_course.uua.mapper.EcourseVideoMapper;
import cn.com.connext.loreal_course.vo.EcourseVideoVo;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
public LYResultVO uploadVideo(MultipartFile file, HttpServletRequest request) {
String msg = "";
String timeLength = "";
Encoder encoder = new Encoder();
EcourseVideoVo vo = new EcourseVideoVo();
long ms = 0;
try {
InputStream inputStream = file.getInputStream();
if (null == inputStream) {
return LYResultVO.errorResultVO("上传文件不能为空");
}
FileType type = FileTypeJudge.getType(inputStream);
if (null == type && type != FileType.MP4 && type != FileType.AVI && type != FileType.RM && !type.equals("MP4")) {
return LYResultVO.errorResultVO("此处只允许上传AVI、MP4和RM格式的视频");
}
File source = MultipartFileToFile.multipartFileToFile(file);
// 获取视频时长
MultimediaInfo m = encoder.getInfo(source);
MultipartFileToFile.delteTempFile(source);
ms = m.getDuration();
// 获取视频时长
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
String strSecond = second < 10 ? "0" + second : "" + second;//秒
if (strHour.equals("00")) {
timeLength += strMinute + ":" + strSecond;
} else {
timeLength += strHour + ":" + strMinute + ":" + strSecond;
}
vo.setTimeLength(timeLength);
String imgUrl = mInitaZureConnection.imgUpload(file);
vo.setUrl(imgUrl);
return LYResultVO.successResultVO(vo, "上传成功");
} catch (Exception e) {
e.printStackTrace();
msg = "" + e;
return LYResultVO.errorResultVO("请上传正确的图片格式", msg);
}
}
package cn.com.connext.loreal_course.utils; /**
- Created by TongGuoBo on 2019/6/19.
*/
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @ClassName MultipartFileToFile
* @Description MultipartFile转fie
* @Author TongGuoBo
* @Date 2019/6/19 13:48
**/
public class MultipartFileToFile {
/**
* MultipartFile 转 File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
//获取流文件
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除本地临时文件
* @param file
*/
public static void delteTempFile(File file) {
if (file != null) {
File del = new File(file.toURI());
del.delete();
}
}
}