springboot接收别人上传的本地视频实例代码

所属分类: 软件编程 / java 阅读数: 33
收藏 0 赞 0 分享
package com.videobackend.controller;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.multipart.MultipartFile;
import com.ty.model.AdminTbl;
import com.ty.model.RolePermissionTbl;
import com.ty.service.PermissionService;
import com.utils.Constants;
import com.utils.ParamterNullCheck;
import com.utils.ToInterface;
import com.videobackend.model.Video;
import com.videobackend.model.VideoRecommend;
@Controller
@RequestMapping(value = "/videoInfo")
public class VideoController {
 /**
 * 调测日志记录器。
 */
 private static final Logger DEBUGGER = Logger.getLogger(VideoController.class);
 @Autowired
 private PermissionService permissionService;
 private static String FILE_ADDRESS;
 @Value("${file_address}")
 public void setfILE_ADDRESS(String fILE_ADDRESS) {
 FILE_ADDRESS = fILE_ADDRESS;
 }
/**
 * 上传本地视频
 * 
 * @param request
 * @param response
 * @param model
 * @param video
 * @param admintabl
 * @return
 * @throws IOException
 * @throws IllegalStateException
 */
 @RequestMapping(value = "/save_local_video", method = RequestMethod.POST)
 @ResponseBody
 public String save_local_video(@RequestParam("file") MultipartFile file, Video video, AdminTbl admintabl)
  throws IllegalStateException, IOException {
 JSONObject result = new JSONObject();
 String[] args = { "admin_id", "cover", "createdTime", "title"};
 JSONObject nullcheck = ParamterNullCheck.getInstance().checkNull(video, args);
 if (!file.isEmpty()) {
  //存放地址
      String path = FILE_ADDRESS;
  //如果父文件夹不存在 则创建文件夹 文件夹为path,视频名字file.getOriginalFilename()
  File filepath = new File(path, file.getOriginalFilename());
  if (!filepath.getParentFile().exists()) {
  filepath.getParentFile().mkdirs();
  }
  File fi = new File(path + File.separator + file.getOriginalFilename());
  //下载到本地
  file.transferTo(fi);
      //获取绝对路径
  String localAddress = fi.getAbsolutePath();
  DEBUGGER.info("存入本地文件地址:" + localAddress);
  video.setLocalAddress(localAddress);
  //获取后缀名
  String suffix= localAddress.substring(localAddress.lastIndexOf("."), localAddress.length());
  DEBUGGER.info("后缀名:" + suffix);
  video.setSuffix(suffix);
  if (nullcheck == null) {
  // 查询该 用户是否有该权限
  admintabl.setUrl("/videoInfo/save_local_video");
  RolePermissionTbl rpt = permissionService.get_permission(admintabl);
  if (rpt != null) {
   JSONObject param = null;
   param = (JSONObject) JSON.toJSON(video);
   DEBUGGER.info(param.toJSONString());
   // 调取接口
   StringBuffer saveLocalVideo = 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);
 }
 return result.toJSONString();
 }

}

yml配置

#设置文件大小 srpingboot不设置会报错
spring.servlet.multipart.max-file-size : 10mb
spring.servlet.multipart.max-request-size : 100mb
#文件存放地址
file_address: D:\\image

pom配置

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
 </dependency>
   <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId>commons-fileupload</artifactId> 
      <version>1.2.2</version> 
    </dependency> 

以上所述是小编给大家介绍的springboot接收别人上传的本地视频,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多