/**
* 上传文件
*
* @param file
* @return
* @throws Exception
*/
@ResponseBody
@PostMapping("addFileForNotice")
@ApiOperation(value = "上传文件for通知公告", notes = "")
public ResultDTO<String> addFileForNotice(HttpServletRequest request, MultipartFile file, String groupId, String fileSourceId,String type) throws Exception {
ResultDTO<String> resultDTO = new ResultDTO<>();
String filePath = null;
// 当前登录人
LoginOperator nowOperDTO = OperatorUtil.getOperator(request);
if (null == nowOperDTO) {
return resultDTO.set(ResultCodeEnum.ERROR_UNKNOW, "请先登陆,才能进行操作!");
}
if(!file.isEmpty()) {
try {
filePath = uploadFileService.addFile2(nowOperDTO, file.getOriginalFilename(), file.getInputStream(), groupId, fileSourceId,type);
} catch (Exception e) {
e.printStackTrace();
return resultDTO.set(ResultCodeEnum.ERROR_UNKNOW, e.getMessage());
}
}else {
return resultDTO.set(ResultCodeEnum.ERROR_UNKNOW, "文件为空!");
}
return resultDTO.set(ResultCodeEnum.SUCCESS, "发布成功",filePath);
}
/**
* 获取一个返回值for 通知公告的详情附件下载链接
* @param nowOperDTO
* @param fileName
* @param fileInputStream
* @param groupId
* @param fileSourceId
* @return
* @throws Exception
*/
String addFile2(LoginOperator nowOperDTO, String fileName, InputStream fileInputStream, String groupId, String fileSourceId,String type) throws Exception;
@Override
public String addFile2(LoginOperator nowOperDTO, String fileName, InputStream fileInputStream, String groupId, String fileSourceId,String type) throws Exception {
String originalFileName = fileName;
fileName = genFileNameOnDisk(fileName);
String targetFilePath = uploadFilePath + File.separator + fileName;
File targetFile = new File(targetFilePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
Files.copy(fileInputStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
SmUploadFile uploadFile = new SmUploadFile();
SmVideo smVideo = new SmVideo();
String filePath = null;
if ("video".equals(type)) {
smVideo.setCreatedDate(new Date());
smVideo.setCreater(nowOperDTO.getOpName());
smVideo.setIsdelete(0);
smVideo.setVideoName(originalFileName);
smVideo.setVideoPath(uploadFilePath + File.separator + fileName);
smVideo.setFileSize(targetFile.length());
sqlManager.insertTemplate(SmVideo.class, smVideo, true);
filePath = smVideo.getVideoPath();
} else {
uploadFile.setFileName(originalFileName);
uploadFile.setFilePath(uploadFilePath + File.separator + fileName);
uploadFile.setFileSize(targetFile.length());
uploadFile.setGroupId(groupId);
uploadFile.setFileSourceId(fileSourceId);
uploadFile.setCreateOpId(nowOperDTO.getOpId());
uploadFile.setCreateDate(new Date());
sqlManager.insertTemplate(SmUploadFile.class, uploadFile, true);
filePath = uploadFile.getFilePath();
}
return filePath;
}
private String genFileNameOnDisk(String fileName){
String prefix = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
String fileNameNew = prefix + "_" + fileName;
return fileNameNew;
}