上传文件
程序员文章站
2024-03-14 14:51:22
...
利用springboot框架里面的spring-core产品id里面已有的jar进行上传文件。
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ResultBean uploadFile(MultipartFile multipart){
if(multipart == null || multipart.isEmpty()){
return new ResultBean().error("500", "文件为空");
}
Pattern pattern = Pattern.compile(virFileNameRegex);
Matcher matcher = pattern.matcher(multipart.getOriginalFilename());
if(!matcher.matches()){
return new ResultBean().error("500", "文件命名有误");
}
//获取当前用户、所在部门、归属分行
UserInfo user = getUser();
Department dept = departmentService.getById(user.getOrgId());
Department cityBranch = departmentService.getCityBranchIdByPrior(dept.getId());
//保存文件
String dirPath = uploadTempPath + File.separator + temDir + File.separator + dept.getNum();
File dir = new File(dirPath);
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(dirPath + File.separator + multipart.getOriginalFilename());
if(file.exists()){
file.delete();
}
try {
FileCopyUtils.copy(multipart.getInputStream(), new FileOutputStream(file));
}catch (Exception e){}
}
上一篇: java MD5加密工具类
下一篇: java和php对等的3DES加密算法