html代码:
<div class="col-md-4 col-sm-4 col-xs-4 col-lg-4">
视频上传 <input type="file" class="form-control" name="file" id="file"><br>
<input type="button" value="上传" id="uploadFile"style="width:724px;height:38px;font-size: 25px;">
</div>
js代码:
var formData = new FormData();
var file = document.getElementsByName('file')[0].files[0];
formData.append('file', file);
formData.append("kemuid",kemu);
formData.append("userid",sessionStorage.getItem("id"));
console.log(formData);
$.ajax({
type : 'post',
url : "/uploadflv/upload",
async : true,
data : formData,
cache : false,
processData : false,
contentType : false,
success : function(res) {
layer.closeAll();
layer.msg('上传成功');
},
error : function(res) {
layer.msg('上传失败');
layer.closeAll();
}
});
后台java代码:
@PostMapping("/upload")
@ResponseBody
public ClientResponse upload( @RequestParam( value="file",required=false) MultipartFile multipartFile, HttpServletRequest request) {
ClientResponse response = ClientResponse.ok();
Video entity = new Video();
//视频上传
//获取原文件名
String fileName=multipartFile.getOriginalFilename();
String name = fileName.substring(0, fileName.lastIndexOf("."));
//获取文件后缀
String subffix=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
// System.out.println(subffix);
//控制格式
/* if(subffix.equals("")||!subffix.equals("mp4")||!subffix.equals("mov")||!subffix.equals("avi")||!subffix.equals("wmv")||!subffix.equals("m4v")||!subffix.equals("dat")||!subffix.equals("flv")||!subffix.equals("mkv"))
{
return -1;
}*/
//新的文件名以日期命名
String NewfileName=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//获取项目路径到webapp
// String filepath=request.getServletContext().getRealPath("/")+"files\\";
//System.out.println(filepath);
//获取项目根路径并转到static/videos
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/video/";
//String path=".src/main/resoures/static/video/";
// System.out.println(path);
File file=new File(path);
if(!file.exists())//文件夹不存在就创建
{
file.mkdirs();
}
//保存文件
try {
multipartFile.transferTo(new File(file+"\\"+NewfileName+"."+subffix));
} catch (IOException e) {
e.printStackTrace();
}
entity.setVideoUrl("../video/"+NewfileName+"."+subffix);
entity.setVideoName(name);
entity.setTbCurriculumId(Integer.parseInt(request.getParameter("kemuid")));
entity.setTbUserId(Integer.parseInt(request.getParameter("userid")));
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
entity.setCreateTime(timestamp);
//response.setData(videoServiceImpl.reportVideo(entity));
String realPath=file+"\\"+fileName+"."+subffix;
String simulationVideo="/videos/"+fileName+"."+subffix;
return response;
}
红色框:写你保存数据库的代码