swagger文件上传处理

用到了文件上传,所以引入相关依赖   

<!-- apache 工具类 -->
     <dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>





@ApiOperation(value = "上传视频", notes = "上传视频接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户id", required = true, 
dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "bgmId", value = "背景音乐id", required = false,
dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "videoSeconds", value = "背景音乐播放长度", required = true,
dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "videowidth", value = "视频宽度", required = true,
dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "videoHeight", value = "视频高度", required = true,
dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "desc", value = "视频描述", required = false,
dataType = "String", paramType = "query"),
})
@PostMapping(value="/upload",headers="content-type=multipart/form-data")
public IMoocJSONResult uploadFace(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:/eclipsework/xiaochengxu/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) {
// TODO Auto-generated catch block
e.printStackTrace();
return IMoocJSONResult.errorMsg("上传出错...");
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}


return IMoocJSONResult.ok();
}

效果:



至于多文件的我想swagger应该不支持吧,反正我是没研究出来。。

猜你喜欢

转载自blog.csdn.net/qq1134585484/article/details/80719243
今日推荐