欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-23 13:27:58
package com.videobackend.controller; import java.io.file; import java.io.ioexcep...
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接收别人上传的本地视频,希望对大家有所帮助