//本地windows系统,项目要部署在linux系统。
public Map<String, Object> uploadFile(HttpServletRequest request, MultipartFile uploadFile, Long id,String targetPath) {
Map<String, Object> resultMap = new HashMap<>();
String path = null;
String os = System.getProperties().getProperty("os.name");
//判断系统是windows,还是linux系统
if (!os.toLowerCase().startsWith("win")) {
// linux
path = targetPath;//-----/home/admin/upload
} else {
path = request.getSession().getServletContext().getRealPath("upload");
}
log.info("========path=========" + path);
String originalFilename = uploadFile.getOriginalFilename();
String fileTyle = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
String newFileName = getFileNameStr() + "." + fileTyle;
long size = uploadFile.getSize();
String[] types = OrganizationConstant.IMG_TYPES;
if (!Arrays.asList(types).contains(fileTyle.toUpperCase())) {
throw new BusinessValidationException(
"请选择" + String.format("%s,%s,%s", OrganizationConstant.IMG_TYPES) + "图片类型");
}
if (size > OrganizationConstant.MAXSIZE) {
throw new BusinessValidationException("图片大于" + OrganizationConstant.MAXSIZE / 1024 + "KB,请重新上传");
}
originalFilename = newFileName;
File targetFile = new File(path, originalFilename);
log.info("路径:" + path + originalFilename);
File pathExist = new File(path);
if (!pathExist.exists()) {
pathExist.mkdirs();
}
try {
uploadFile.transferTo(targetFile);
SystemPlat system = new SystemPlat();
system.setId(id);
system.setLogoUrl(File.separator + originalFilename);
systemMapper.updateSystem(system);
resultMap.put("id", id);
resultMap.put("success", true);
} catch (Exception e) {
throw new ServiceValidationException("上传图片出错!", e);
}
return resultMap;
}