video.wxml 页面
<form bindsubmit='upload'>
<view class="inputView">
<label class="loginLabel">视频描述:</label>
<input name="desc" class="inputText" placeholder="说点什么吧" />
</view>
<!-- 提交 -->
<button class="submitBtn" type="primary" form-type='submit'>上传视频</button>
<button class="gobackBtn" type="warn" form-type='reset'>重置</button>
</form>
在video.js里定义video.wxml 页面 <form bindsubmit='upload'>upload方法
upload: function(e){
var me = this;
var bgmId = e.detail.value.bgmId;
var desc = e.detail.value.desc;
var duration = me.data.videoParams.duration;
var tmpHeight = me.data.videoParams.tmpHeight;
var tmpWidth = me.data.videoParams.tmpWidth;
var tmpVideoUrl = me.data.videoParams.tmpVideoUrl;
var tmpCoverUrl = me.data.videoParams.tmpCoverUrl;
//上传短视频
wx.showToast({
title: '上传中...',
})
var serverUrl = app.serverUrl;
wx.uploadFile({
url: serverUrl + '/video/upload',
formData: {
userId: app.userInfo.id,
bgmId: bgmId,
desc: desc,
videoSeconds: duration,
videoHeight: tmpHeight,
videoWidth: tmpWidth
},
filePath: tmpVideoUrl,
name: 'file',
header: {
'content-type': 'application/json', // 默认值
},
success(res) {
var data = JSON.parse(res.data);
console.log(res);
wx.hideLoading();
if (data.status == 200) {
wx.showToast({
title: '上传成功',
icon: 'success',
duration: 3000,
});
}
}
})
}
controller层
package com.imooc.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.imooc.utils.IMoocJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@RestController
@Api(value="视频相关业务的接口", tags="视频相关业务的controller")
@RequestMapping("/video")
public class VideoController {
@ApiOperation(value="上传视频", notes="上传视频接口")
@ApiImplicitParams({
@ApiImplicitParam(name="userId", value="用户id",required=true,dataType="String",paramType="form"),
@ApiImplicitParam(name="bgmId", value="背景音乐id",required=false,dataType="String",paramType="form"),
@ApiImplicitParam(name="videoSeconds", value="背景音乐播放长度",required=true,dataType="String",paramType="form"),
@ApiImplicitParam(name="videoWidth", value="视频宽度",required=true,dataType="String",paramType="form"),
@ApiImplicitParam(name="videoHeight", value="视频高度",required=true,dataType="String",paramType="form"),
@ApiImplicitParam(name="desc", value="视频描述",required=false,dataType="String",paramType="form")
})
@PostMapping(value="/upload",headers="content-type=multipart/form-data")
public IMoocJSONResult upload(String userId,
String bgmId, double videoSeconds, int videoWidth, int videoHeight, String desc,
@ApiParam(value="短视频",required=true) MultipartFile file) throws Exception {
if (StringUtils.isBlank(userId)) {
return IMoocJSONResult.errorMsg("用户id不能为空...");
}
//文件保存命名空间
String fileSpace = "D:/imooc_videos_dev";
//保存到数据库中的相对路径
String uploadPathDB = "/" + userId + "/video";
FileOutputStream fileOutputStream = null;
InputStream inputStream = null;
try {
if(file != null ) {
//获取上传文件为原名
String fileName = file.getOriginalFilename();
if(StringUtils.isNotBlank(fileName)) {
//文件上传保存的路径
String finalVideopath = fileSpace + uploadPathDB + "/" +fileName;
//设置数据库保存的路径
uploadPathDB += ("/" + fileName);
File outFile = new File(finalVideopath);
//判断文件夹是否存在
if(outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
//创建父文件夹
outFile.getParentFile().mkdirs();
}
fileOutputStream = new FileOutputStream(outFile);
inputStream = file.getInputStream();
IOUtils.copy(inputStream, fileOutputStream);
}
}else{
return IMoocJSONResult.errorMsg("上传出错...");
}
} catch (Exception e) {
e.printStackTrace();
return IMoocJSONResult.errorMsg("上传出错...");
} finally {
if(fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
return IMoocJSONResult.ok();
}
}
Swagger2最终展示的结果