@RestController @RequestMapping(value = "/uploadfile") public class UpLoadFileController { private static Logger logger = LoggerFactory.getLogger(UpLoadFileController.class); /** * 上传图片 * @param file * @return * @throws Exception */ @PostMapping("/image/upload") public ResultModel<Map<String,Object>> imgUpload(@RequestParam("imgFile")MultipartFile file) throws Exception{ Map<String, Object> map = new HashMap<String, Object>(); String fileName = file.getOriginalFilename(); String uuid = UuidUtil.getUuid(); String filePath = "files/img/"+ DateTimeUtil.getNowDateStr(DateTimeUtil.yyyy_MM_dd)+ "/"+uuid+"/"; String rearllyfilePath = CommonConstant.FILE_ROOT_PATH + filePath; //获取文件后缀名 String [] lastName = fileName.split("\\."); if(lastName.length<2){ throw new MyException("无法获取文件后缀名!请重试!"); } if(Common.isnull(file) || file.isEmpty()){ throw new MyException("无法读取图片请重试!"); } //大于2M不能保存 if(file.getSize()>10240000){ throw new MyException("图片大于10M无法上传!"); } if(!fileName.endsWith(".jpg") && !fileName.endsWith(".png") && !fileName.endsWith(".gif")&& !fileName.endsWith(".svg")&& !fileName.endsWith(".jpeg")) { throw new MyException("图片上传失败!请上传jpg、png、jpeg、svg、gif格式的图片!"); } //返回 map.put("image", filePath+fileName); map.put("fileName", fileName); map.put("saveAddress", rearllyfilePath); try { uploadFile(file.getBytes(), rearllyfilePath, fileName); } catch (Exception e) { logger.error("图片上传失败!原因={}",e.getMessage()); throw new MyException("图片上传失败!"); } return new ResultModel<Map<String,Object>>(map); } /** * 上传文件 * @param file * @return * @throws Exception */ @PostMapping("/file/upload") public ResultModel<Map<String,Object>> fileUpload(@RequestParam("file")MultipartFile file) throws Exception{ Map<String, Object> map = new HashMap<String, Object>(); String fileName = file.getOriginalFilename(); String uuid = UuidUtil.getUuid(); String filePath = "files/file/"+ DateTimeUtil.getNowDateStr(DateTimeUtil.yyyy_MM_dd)+ "/"+uuid+"/"; String rearllyfilePath = CommonConstant.FILE_ROOT_PATH + filePath; //获取文件后缀名 String [] lastName = fileName.split("\\."); if(Common.isnull(file) || file.isEmpty()){ throw new MyException("无法读取文件请重试!"); } //大于10M不能保存 if(file.getSize()>1024*10240){ throw new MyException("文件大小超过10M无法上传!"); } if(Common.isnull(lastName) || Common.isnull(lastName[lastName.length-1])){ throw new MyException("非法文件,无法上传!"); } if(CommonConstant.FILE_END_NAMES.indexOf(lastName[lastName.length-1].toLowerCase())==-1) { throw new MyException("非法文件,无法上传!"); } //返回 map.put("url", filePath+fileName); map.put("fileName", fileName); map.put("saveAddress", rearllyfilePath); try { uploadFile(file.getBytes(), rearllyfilePath, fileName); } catch (Exception e) { logger.error("文件上传失败!原因={}",e.getMessage()); throw new MyException("文件上传失败!"); } return new ResultModel<Map<String,Object>>(map); } /** * 新建文件夹,保存文件 * @param file * @param filePath * @param fileName * @throws Exception */ public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception{ //设置文件路径 File targetFile = new File(filePath); //判断路径是否已经存在 如果不存在就新建 if(!targetFile.exists()) { targetFile.mkdirs(); } //写入文件 FileOutputStream out = new FileOutputStream(filePath + fileName); out.write(file); out.flush(); out.close(); } }
java文件上传控制器
最新推荐文章于 2024-04-05 23:10:53 发布