标签:inputStream,java,String,FilePath,fileOut,前端,jobId,null,上传 From: https://www.cnblogs.com/Lee597/p/18205575
/**
* 获取上传图片
*/
@PostMapping("/getImage")
@ApiOperationSupport(order = 1)
@ApiOperation(value = "获取图片", notes = "保存本地")
public R Stringuploadtaskpic(MultipartFile multipartFile, String jobId, HttpServletRequest request) throws IOException {
//region 获取上传图片
String fileName = multipartFile.getOriginalFilename();
System.out.println("完整文件名 = " + fileName);
InputStream inputStream = null;
FileOutputStream fileOut = null;
String UUID = StringUtil.getUUID();
String FilePath = null;
if (fileName != null) {
FilePath = LauncherConstant.IMAGE_FILE_ADDR + UUID + fileName.substring(fileName.lastIndexOf("."));
}
try {
inputStream = multipartFile.getInputStream();
if (FilePath != null) {
fileOut = new FileOutputStream(FilePath);
}else return R.success("已存在该文件");
IOUtils.copy(inputStream, fileOut);
String originalFilePath = supperMapper.selectImageAddr(jobId);
if (originalFilePath == null) {
supperMapper.insertImageAddr(jobId, FilePath);
} else {
File file = new File(originalFilePath);
if (file.exists() && file.isFile()) {
boolean flag = file.delete();
if (flag) {
supperMapper.updateImageAddr(jobId, FilePath);
} else return R.success("上传出错,请重新上传");
}
}
if (fileOut != null) {
fileOut.flush();
}
} catch (IOException e) {
log.info(e.getMessage());
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (fileOut != null) {
fileOut.close();
}
} catch (IOException e) {
log.info(e.getMessage());
}
}
return R.success(jobId + "-" + UUID);
//endregion
}