springboot接收别人上传的本地视频实例代码
作者:qq_33931552 时间:2023-03-22 01:55:39
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接收别人上传的本地视频网站的支持!
来源:https://blog.csdn.net/qq_33931552/article/details/81299235
标签:spring,boot,本地视频,上传
0
投稿
猜你喜欢
登录验证全局控制的几种方式总结(session)
2022-09-03 15:25:41
Java设计模式之原型设计示例详解
2023-08-04 04:53:35
IDEA设置生成带注释的getter和setter的图文教程
2023-06-28 00:49:36
Kotlin扩展方法超详细介绍
2023-05-31 16:31:23
C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法
2023-06-30 12:55:22
基于Java实现获取本地IP地址和主机名
2023-03-19 04:03:55
Java方法重载和重写原理区别解析
2022-09-28 14:29:04
C# Winform 子窗体访问父级窗体的控件和属性
2022-08-15 04:37:06
java实现工资管理简单程序
2021-12-29 22:40:58
SpringMVC请求数据详解讲解
2021-09-13 08:56:56
Android实现自定义圆形进度条
2022-10-28 04:55:22
Java并发系列之AbstractQueuedSynchronizer源码分析(共享模式)
2022-06-03 13:39:58
Android使用开源组件PagerBottomTabStrip实现底部菜单和顶部导航功能
2023-07-02 11:32:40
android效果TapBarMenu绘制底部导航栏的使用方式示例
2023-07-29 20:53:36
通过Mybatis实现单表内一对多的数据展示示例代码
2021-12-20 12:34:31
使用Spring Boot 2.x构建Web服务的详细代码
2022-09-17 04:08:40
Android提高之MediaPlayer音视频播放
2021-05-29 08:44:37
在Flutter中制作翻转卡片动画的完整实例代码
2023-06-23 23:31:21
解读@RequestBody与post请求的关系
2022-10-07 02:02:51
详解如何热更新线上的Java服务器代码
2021-05-26 01:29:33