springboot接收别人上传的本地视频实例代码
packagecom.videobackend.controller;
importjava.io.File;
importjava.io.IOException;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.log4j.Logger;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.bind.annotation.ResponseBody;
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONObject;
importorg.springframework.web.multipart.MultipartFile;
importcom.ty.model.AdminTbl;
importcom.ty.model.RolePermissionTbl;
importcom.ty.service.PermissionService;
importcom.utils.Constants;
importcom.utils.ParamterNullCheck;
importcom.utils.ToInterface;
importcom.videobackend.model.Video;
importcom.videobackend.model.VideoRecommend;
@Controller
@RequestMapping(value="/videoInfo")
publicclassVideoController{
/**
*调测日志记录器。
*/
privatestaticfinalLoggerDEBUGGER=Logger.getLogger(VideoController.class);
@Autowired
privatePermissionServicepermissionService;
privatestaticStringFILE_ADDRESS;
@Value("${file_address}")
publicvoidsetfILE_ADDRESS(StringfILE_ADDRESS){
FILE_ADDRESS=fILE_ADDRESS;
}
/**
*上传本地视频
*
*@paramrequest
*@paramresponse
*@parammodel
*@paramvideo
*@paramadmintabl
*@return
*@throwsIOException
*@throwsIllegalStateException
*/
@RequestMapping(value="/save_local_video",method=RequestMethod.POST)
@ResponseBody
publicStringsave_local_video(@RequestParam("file")MultipartFilefile,Videovideo,AdminTbladmintabl)
throwsIllegalStateException,IOException{
JSONObjectresult=newJSONObject();
String[]args={"admin_id","cover","createdTime","title"};
JSONObjectnullcheck=ParamterNullCheck.getInstance().checkNull(video,args);
if(!file.isEmpty()){
//存放地址
Stringpath=FILE_ADDRESS;
//如果父文件夹不存在则创建文件夹文件夹为path,视频名字file.getOriginalFilename()
Filefilepath=newFile(path,file.getOriginalFilename());
if(!filepath.getParentFile().exists()){
filepath.getParentFile().mkdirs();
}
Filefi=newFile(path+File.separator+file.getOriginalFilename());
//下载到本地
file.transferTo(fi);
//获取绝对路径
StringlocalAddress=fi.getAbsolutePath();
DEBUGGER.info("存入本地文件地址:"+localAddress);
video.setLocalAddress(localAddress);
//获取后缀名
Stringsuffix=localAddress.substring(localAddress.lastIndexOf("."),localAddress.length());
DEBUGGER.info("后缀名:"+suffix);
video.setSuffix(suffix);
if(nullcheck==null){
//查询该用户是否有该权限
admintabl.setUrl("/videoInfo/save_local_video");
RolePermissionTblrpt=permissionService.get_permission(admintabl);
if(rpt!=null){
JSONObjectparam=null;
param=(JSONObject)JSON.toJSON(video);
DEBUGGER.info(param.toJSONString());
//调取接口
StringBuffersaveLocalVideo=ToInterface.interfaceUtil("/video/saveLocalVideo",param.toJSONString(),
"POST");
result.put("saveLocalVideo",saveLocalVideo);
}else{
result.put("msg",Constants.NO_AUTH);
}
}else{
result=nullcheck;
}
}else{
DEBUGGER.info("缺少的参数key======="+file.getName());
result.put("msg",Constants.SYS_PARAMTER_MISSING);
}
returnresult.toJSONString();
}
}
yml配置
#设置文件大小srpingboot不设置会报错 spring.servlet.multipart.max-file-size:10mb spring.servlet.multipart.max-request-size:100mb #文件存放地址 file_address:D:\\image
pom配置
org.springframework.boot spring-boot-configuration-processor true commons-fileupload commons-fileupload 1.2.2
总结
以上所述是小编给大家介绍的springboot接收别人上传的本地视频,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!