/**
* 文件上传(图片或视频)
* @param file
* @return
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFile (@RequestParam(value = "file") MultipartFile file) {
Map<String, Object> map = algorithmService.uploadFile(file);
return JSON.toJSONString(map);
}
public Map<String, Object> uploadFile(MultipartFile file) {
Map<String, Object> map = new HashMap<>(4);
try {
if (file.isEmpty()) {
log.error("上传的文件是空的");
map.put("code", 201);
return map;
}
//获取原始文件名及扩展名
String originalFileName = file.getOriginalFilename();
String extension = getExtensionName(originalFileName);//返回结果如 jpg
//生成新的文件名
String fileName = System.currentTimeMillis() + "." + extension;
//保存路径
File imageSavePath = new File(HongLianConstants.UPLOAD_IMAGE_PATH);
if (!imageSavePath.exists()) {
imageSavePath.mkdirs();
}
String savePath = HongLianConstants.UPLOAD_IMAGE_PATH + "/" + fileName;
log.info("上传文件的全路径 {}", savePath);
File file1 = new File(savePath);
FileOutputStream outputStream = new FileOutputStream(file1);
outputStream.write(file.getBytes());
Map<String, String> dataMap = new HashMap<>();
dataMap.put("fileName", fileName);
map.put("code", 200);
map.put("message", "success");
map.put("data", dataMap);
log.info("文件保存完成,保存的文件路径为 {}", savePath);
} catch (Exception e) {
log.error("uploadImage error {}", e);
}
return map;
}