Commit 0ca21c2e authored by m1991's avatar m1991

资讯模块:用户 图片/视频,文字上传功能实现完成

parent bd9e8c73
......@@ -111,6 +111,37 @@
<version>2.2.6</version>
</dependency>
<!-- 视频获取第一帧 -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>0.8</version>
</dependency>
<!--&lt;!&ndash;druid数据源 &ndash;&gt;-->
<!--<dependency>-->
<!--<groupId>com.alibaba</groupId>-->
<!--<artifactId>druid</artifactId>-->
<!--<version>1.1.9</version>-->
<!--</dependency>-->
<!--&lt;!&ndash; 文件上传 &ndash;&gt;-->
<!--<dependency>-->
<!--<groupId>commons-fileupload</groupId>-->
<!--<artifactId>commons-fileupload</artifactId>-->
<!--<version>1.3.3</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>commons-io</groupId>-->
<!--<artifactId>commons-io</artifactId>-->
<!--<version>2.4</version>-->
<!--</dependency>-->
<!-- 阿里巴巴JSON处理器 -->
<!--<dependency>-->
<!--<groupId>com.alibaba</groupId>-->
<!--<artifactId>fastjson</artifactId>-->
<!--<version>1.2.31</version>-->
<!--</dependency>-->
</dependencies>
<!-- MVN构建插件 -->
......
package cn.wisenergy.common.utils;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
/**
* 截取视频图片
* @ClassName: FrameGrabberKit
*/
public class FrameGrabberKit {
/**
* 获取视频图片
* @param videofile 源视频文件路径
* @param framefile 截取帧的图片存放路径
* @param imgfile 视频封面图保存路径
* @return
*/
public static String getVedioImg(String videofile, String framefile,String imgfile){
String ImgUrl="";
//截取封面图
try {
fetchFrame(videofile, framefile);
} catch (Exception e) {
e.printStackTrace();
}
// 完整的ImgUrl
ImgUrl = imgfile;//视频封面图保存路径
return ImgUrl;
}
/**
* 获取指定视频的帧并保存为图片至指定目录
* @param videofile 源视频文件路径
* @param framefile 截取帧的图片存放路径 例:F:\hfkjrecorder\target\4.jpg
* @throws Exception
*/
public static void fetchFrame(String videofile, String framefile) throws Exception {
long start = System.currentTimeMillis();
File targetFile = new File(framefile);
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
ff.start();
int lenght = ff.getLengthInFrames();
int i = 0;
int interceptionFrames = 30;//截取第几帧
//默认截取第50帧,如果第50帧大于视频总帧数的8成直接取长度lenght * 0.3
if(interceptionFrames >= lenght * 0.8) {
interceptionFrames = (int)(lenght * 0.3);
}
Frame f = null;
while (i < lenght) {
// 过滤 前 interceptionFrames 帧,避免出现全黑的图片,依自己情况而定
f = ff.grabFrame();
if ((i > interceptionFrames) && (f.image != null)) {
break;
}
i++;
}
opencv_core.IplImage img = f.image;
int owidth = img.width();
int oheight = img.height();
// 对截取的帧进行等比例缩放 宽350、高160
// if(owidth > oheight) {//宽大于高
//
// }else {//高大于宽
//
// }
int width = 800;
int height = (int) (((double) width / owidth) * oheight);
/**
width - 所创建图像的宽度
height - 所创建图像的高度
imageType - 所创建图像的类型
TYPE_3BYTE_BGR - 表示一个具有 8 位 RGB 颜色分量的图像,对应于 Windows 风格的 BGR 颜色模型,具有用 3 字节存储的 Blue、Green 和 Red 三种颜色。
*/
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
//此方法返回 Graphics2D,但此处是出于向后兼容性的考虑。
bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null);
ImageIO.write(bi, "jpg", targetFile);
ff.flush();
ff.stop();
System.out.println(System.currentTimeMillis() - start);
}
}
package cn.wisenergy.mapper;
import cn.wisenergy.model.app.User;
import cn.wisenergy.model.app.shopZx;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.TargetType;
import java.util.List;
import java.util.Map;
/**
* Created by m1991 on 2021/2/24 14:50
*/
@Mapper
public interface ShopZxMapper extends BaseMapper<shopZx> {
/**
* 添加方法 zxUrl userId zxName zxField imgUrl
* @param
* @return 1
*/
int zxadd(@Param("zxUrl") String zxUrl,@Param("userId") String userId,@Param("zxName") String zxName,@Param("zxField") String zxField,@Param("imgUrl") String imgUrl,@Param("zxAddress") String zxAddress);
/**查询方法
* @param
* @return
*/
List<shopZx> selectAll();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.wisenergy.mapper.ShopZxMapper">
<resultMap id="zxMap" type="cn.wisenergy.model.app.shopZx">
<id column="zxid" property="zxid"/>
<result column="zxUrl" property="zxUrl"/>
<result column="zxLikes" property="zxLikes"/>
<result column="userid" property="userid"/>
<result column="zxName" property="zxName"/>
<result column="zxShenHe" property="zxShenHe"/>
<result column="zxField" property="zxField"/>
<result column="zxAddress" property="zxAddress"/>
<result column="imgUrl" property="imgUrl"/>
</resultMap>
<sql id="table">
shop_zx
</sql>
<sql id="cols_all">
zxid,
<include refid="cols_exclude_id"/>
</sql>
<sql id="cols_exclude_id">
zxUrl,userId,zxName,zxField,imgUrl,zxAddress
</sql>
<sql id="vals">
#{zxUrl},#{userId},#{zxName},#{zxField},#{imgUrl},#{zxAddress}
</sql>
<!--资讯内容插入-->
<insert id="zxadd" parameterType="cn.wisenergy.model.app.shopZx">
insert into
<include refid="table"/>
(<include refid="cols_exclude_id"/>)
value(
<include refid="vals"/>
)
</insert>
<!--资讯内容插入-->
<!--<insert id="zxadd" parameterType="cn.wisenergy.model.app.shopZx" >-->
<!--INSERT INTO-->
<!--(zxUrl, userId,zxName,zxField,imgUrl)-->
<!--VALUES(-->
<!--#{zxUrl},#{userid},#{zxName},#{zxField},#{imgUrl}-->
<!--)-->
<!--</insert>-->
<!--资讯内容倒叙查询-->
<select id="selectAll" parameterType="cn.wisenergy.model.app.shopZx" >
select zxid as zxid,zxUrl as zxUrl,
zxLikes as zxLikes,userid as userid,zxName as zxName,
zxShenHe as zxShenHe,zxField as zxField,zxDate as zxDate,
imgUrl as imgUrl,zxAddress as zxAddress from shop_zx order by zxid desc
</select>
</mapper>
\ No newline at end of file
......@@ -28,7 +28,7 @@ public class shopZx extends Model<shopZx> implements Serializable{
private Integer zxid;
/**
* 视频/图片存放地址
* 视频存放地址
*/
@ApiModelProperty(name = "zxUrl", value = "视频/图片存放地址")
private String zxUrl;
......@@ -64,11 +64,23 @@ public class shopZx extends Model<shopZx> implements Serializable{
private String zxField;
/**
* 资讯文字输入字段
* 资讯数据创建时间
*/
@ApiModelProperty(name = "zxField", value = "资讯数据创建时间")
@ApiModelProperty(name = "zxDate", value = "资讯数据创建时间")
private Data zxDate;
/**
* 图片存放地址
*/
@ApiModelProperty(name = "imgUrl", value = "图片存放地址")
private String imgUrl;
/**
* 用户发布地址
*/
@ApiModelProperty(name = "zxAddress", value = "用户发布地址")
private String zxAddress;
public Data getZxDate() {
return zxDate;
}
......
package cn.wisenergy.service.app;
import cn.wisenergy.model.app.shopZx;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
* Created by m1991 on 2021/2/24 13:49
*/
public interface UploadService {
/**
* 视频文件上传 zxUrl, userId,zxName,zxField,imgUrl
* @param request
* @return
*/
public Map<String, Object> uploadVideo(MultipartFile file,HttpServletRequest request,String userId,String zxField,String zxAddress) throws Exception;
/**
* 图片文件上传
* @param request
* @return
*/
public Map<String, Object> uploadImage(MultipartFile file, HttpServletRequest request,String userId,String zxField,String zxAddress) throws Exception;
/**
* 项目目录下的图片文件上传
* @param request
* @return
*/
public Map<String, Object> getImageUrl(HttpServletRequest request) throws Exception;
/**
* 查询资讯所有信息(分页——10条信息一页)
* @param
* @return
*/
List<shopZx> getList();
}
package cn.wisenergy.service.app.impl;
import cn.wisenergy.common.utils.FrameGrabberKit;
import cn.wisenergy.model.app.shopZx;
import cn.wisenergy.service.app.UploadService;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import cn.wisenergy.mapper.ShopZxMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;
/**
* Created by m1991 on 2021/2/24 13:42
*/
@Slf4j
@Transactional
@Service("UploadService")
public class UploadServiceImpl implements UploadService {
@Autowired
private ShopZxMapper shopZxMapper;
/**
* 视频文件上传
*/
@Override
public Map<String, Object> uploadVideo(MultipartFile file,HttpServletRequest request,String userId,String zxField,String zxAddress) throws Exception {
Map<String, Object> resultMap=new HashMap<String, Object>();
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort()+"/mimi/upload/video/";
Long time = new Date().getTime();
String zxName = file.getOriginalFilename();//文件原始名称
String suffixName = zxName.substring(zxName.lastIndexOf("."));//从最后一个.开始截取。截取zxName的后缀名
String newzxName = time+suffixName; //文件新名称
//设置文件存储路径,可以存放在你想要指定的路径里面
String rootPath="D:/mimi/"+File.separator+"upload/video/"; //上传视频存放位置
String filePath = rootPath+newzxName;
File newFile = new File(filePath);
//判断目标文件所在目录是否存在
if(!newFile.getParentFile().exists()){
//如果目标文件所在的目录不存在,则创建父目录
newFile.getParentFile().mkdirs();
}
//将内存中的数据写入磁盘
file.transferTo(newFile);
//视频上传保存url
String zxUrl = basePath + newzxName;
//视频封面图处理
String newImgName = time+".jpg";
String framefile = rootPath + newImgName;
String imgUrlSave = basePath+newImgName;//图片最终位置路径
//视频截取封面图
String imgUrl=FrameGrabberKit.getVedioImg(zxUrl, framefile, imgUrlSave);
resultMap.put("zxUrl", zxUrl);
resultMap.put("imgUrl", imgUrl);
resultMap.put("returnCode", 0);
System.out.println("上传的文件名为:"+zxName+",后缀名为:"+newzxName);
shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
return resultMap;
}
/**
* 图片文件上传
*/
@Override
public Map<String, Object> uploadImage(MultipartFile file, HttpServletRequest request,String userId,String zxField, String zxAddress) throws Exception {
Map<String, Object> resultMap=new HashMap<String, Object>();
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort()+"/mimi/upload/images/";
Long time = new Date().getTime();
String zxName = file.getOriginalFilename();//文件原始名称
String suffixName = zxName.substring(zxName.lastIndexOf("."));//从最后一个.开始截取。截取zxName的后缀名
String newzxName = time+suffixName; //文件新名称
//设置文件存储路径,可以存放在你想要指定的路径里面
String rootPath="D:/mimi/"+File.separator+"upload/images/"; //上传图片存放位置
String filePath = rootPath+newzxName;
File newFile = new File(filePath);
//判断目标文件所在目录是否存在
if(!newFile.getParentFile().exists()){
//如果目标文件所在的目录不存在,则创建父目录
newFile.getParentFile().mkdirs();
}
//将内存中的数据写入磁盘
file.transferTo(newFile);
//图片上传保存url
String imgUrl = basePath + newzxName;
resultMap.put("imgUrl", imgUrl);
resultMap.put("returnCode", 0);
String zxUrl="";
shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
return resultMap;
}
/**
* 项目目录下的图片文件上传
*/
@Override
public Map<String, Object> getImageUrl(HttpServletRequest request) throws Exception {
Map<String,Object> result = new HashMap<String,Object>();
//获取图片在项目路径下的地址
String basePath= ClassUtils.getDefaultClassLoader().getResource("").getPath();
String oldPath=basePath+"/static/images/animate.png";
Long res =new Date().getTime();
//设置文件存储路径,可以存放在你想要指定的路径里面
String rootPath="D:/mimi/"+File.separator+"upload/images/";
// 新文件名
String newzxName =res + oldPath.substring(oldPath.lastIndexOf("."));
//新文件
File newFile=new File(rootPath+File.separator+newzxName);
//判断文件目录是否存在
if(!newFile.getParentFile().exists()){
//如果目标文件所在的目录不存在,则创建父目录
newFile.getParentFile().mkdirs();
}
//-------把图片文件写入磁盘 start ----------------
FileOutputStream fos = new FileOutputStream(newFile);
//读取本地文件
File localFile = new File(oldPath);
//获取本地文件输入流
InputStream stream=new FileInputStream(localFile);
//写入目标文件
byte[] buffer=new byte[1024*1024];
int byteRead=0;
//stream.read(buffer) 每次读到的数据存放在 buffer 数组中
while((byteRead=stream.read(buffer))!=-1){
//在 buffer 数组中 取出数据 写到 (输出流)磁盘上
fos.write(buffer, 0, byteRead);
fos.flush();
}
fos.close();
stream.close();
//-------把图片文件写入磁盘 end ----------------
//服务器图片地址
String baseURL = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort()+"/mimi/upload/images/";
String imgUrl=baseURL+newzxName;
result.put("imgUrl", imgUrl);
result.put("returnCode", 0);
return result;
}
/**
* 获取资讯分页数据
* @return
*/
@Override
public List<shopZx> getList() {
return null;
}
}
package cn.wisenergy.web.admin.controller.app;
import cn.wisenergy.service.app.UploadService;
import cn.wisenergy.service.app.impl.UploadServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Created by m1991 on 2021/2/24 13:46
*/
@Api(tags = "资讯管理")
@RestController
@RequestMapping("/ZX")
@Slf4j
public class UploadController {
@Autowired
private UploadService uploadService;
/**
* @param request
* @return
* @throws Exception
*/
/**
* 视频文件上传
* 资讯主键id* 视频/封面存放地址* 用户id(作者id)
* 获赞数* 视频/图片名称* 审核字段* 资讯文字输入字段
* 资讯创建时间* 图片储存地址* 资讯发表地址
* @param file
* @param request
* @param userId
* @param zxField
* @param zxAddress
* @return
* @throws Exception
*/
@ApiOperation(value = "上传视频,返回路径给前台", notes = "上传视频,返回路径给前台", httpMethod = "POST")
@ApiImplicitParams({
@ApiImplicitParam(name = "zxField", value = "资讯文字", required = false, dataType = "String"),
@ApiImplicitParam(name = "userId", value = "用户手机号", required = true, dataType = "String") })
@RequestMapping(value ="/uploadVideo",method=RequestMethod.POST)
public Map<String, Object> uploadVideo(MultipartFile file,HttpServletRequest request,String userId,String zxField,String zxAddress) throws Exception{
return uploadService.uploadVideo(file, request,userId,zxField,zxAddress);
}
/**
* 图片文件上传
*
* @param request
* @return
* @throws Exception
*/
@ApiOperation(value = "上传图片,返回路径给前台", notes = "上传图片,返回路径给前台", httpMethod = "POST")
@ApiImplicitParams({
@ApiImplicitParam(name = "zxField", value = "资讯文字", required = false, dataType = "String"),
@ApiImplicitParam(name = "userId", value = "用户手机号", required = true, dataType = "String") })
@RequestMapping(value ="/uploadImage",method= RequestMethod.POST)
public Map<String, Object> uploadImage(MultipartFile file, HttpServletRequest request,String userId,String zxField,String zxAddress) throws Exception{
return uploadService.uploadImage(file, request ,userId ,zxField,zxAddress);
}
}
......@@ -8,6 +8,9 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
......@@ -19,4 +22,5 @@ public class MyBatisPlusConfig {
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment