java如何实现本地视频上传功能?需要建立哪些数据包呢?急,因为刚刚学,所以不很懂。如果要加代码应该在哪里添加?
2条回答 默认 最新
- 夜半无声 2017-03-27 03:13关注
后端代码
/* * * 本地视频上传 * */ @RequestMapping(value = "/upVideo") public void videosUp(@RequestParam("file") MultipartFile files, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String fileName = files.getOriginalFilename(); String videoSource = request.getParameter("videoSource"); String videoType = request.getParameter("videoType"); String imageName = request.getParameter("imageName"); // 路径 String filePath = request.getSession().getServletContext().getRealPath("/") + DicConfigUtils.getConfigValue("videoPath", BsConstants.SOFTTYPE_VIP, "0"); File f = new File(filePath); if (!f.exists()) { f.mkdirs(); } files.transferTo(new File(filePath + "/" + fileName)); // 转存文件 Video video = new Video(); video.setvId(IdGenerator.getAccessid("")); video.setCreateDate(CommonUtils.getCurrentTime()); video.setvSource(videoSource); video.setvType(videoType); video.setVideoPath(fileName); video.setImageName(imageName); videoService.create(video); } catch (Exception e) { logger.error("上传视频异常:", e); } }
前端提交:
$("#submit").click(function() { var webcamSources = $("#webcamSources").val(); var webcamTypes = $("#webcamTypes").val(); var imageName = $("#imageName").val(); var file = $("#file").val(); var maxsize = 20*1024*1024;//20M if(webcamSources==""){ layer.msg("请选择视频来源!",{icon: 6,time:1000}); return false; }else if(webcamTypes==""){ layer.msg("请选择视频类型!",{icon: 6,time:1000}); return false; }else if(imageName==""){ layer.msg("请输入视频名称!",{icon: 6,time:1000}); return false; }else if(file==""){ layer.msg("请选择要上传的视频文件!",{icon: 6,time:1000}); return false; }else{ var obj_file = document.getElementById("file"); var filesize = obj_file.files[0].size; if(filesize>maxsize){ layer.msg("导入视频太大,请导入小于20M的视频!",{icon: 6,time:1000}); return false; }else{ $.ajaxFileUpload({ url : '${contextpath}/manage/video/upVideo.html', secureuri : false, fileElementId : 'file', type: 'POST', data:{'imageName':imageName,'videoType':webcamTypes,'videoSource':webcamSources}, contentType : "multipart/form-data; charset=UTF-8", success : function() { layer.msg("上传成功", { icon : 6, time : 1000 }, function() { parent.window.location.href = 'toVideoList.html'; }); }, error : function(e) { layer.msg("上传失败", {icon : 6,time : 1000}); } }) } } });
前端页面:
<span class="btn-upload form-group"> <input class="input-text upload-url" type="text" name="uploadfile-2" id="submit" readonly datatype="*" nullmsg="请添加附件!" style="width:200px"> <a href="javascript:void();" class="btn btn-primary upload-btn"><i class="Hui-iconfont"></i> 浏览文件</a> <input type="file" multiple id="file" name="file" class="input-file"> </span>
我是直接传到服务器目录上的;可以参考下;
解决 2无用 1
悬赏问题
- ¥15 安卓adb backup备份应用数据失败
- ¥15 eclipse运行项目时遇到的问题
- ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
- ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
- ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
- ¥50 成都蓉城足球俱乐部小程序抢票
- ¥15 yolov7训练自己的数据集
- ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
- ¥15 电力市场出清matlab yalmip kkt 双层优化问题
- ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)