1. 程式人生 > >swagger檔案上傳處理

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應該不支援吧,反正我是沒研究出來。。

本人做的學習網站,如果大家條件允許,支援下注冊個會員,感激不盡 ------> 資料最全的學習網站