public void uploadHeader(MultipartFile image_file) {//图片加载公用方法 String originalFilename = image_file.getOriginalFilename(); UUID uuid = UUID.randomUUID();//32位 字母和数字的组合 - - - - String fileName = uuid.toString().replace("-", ""); String pathFile = fileName + originalFilename; File file = new File("D:\\images\\" + pathFile); try { image_file.transferTo(file); } catch (IOException e) { e.printStackTrace(); } User user = userService.queryById(id); user.setHeadUrl("http://localhost:8083/images/" + pathFile); userService.update(user); //return "redirect:/front/user/regist.do"; }
首先配置一个服务器进行存放图片,然后在图片存放的方法里面调用此方法,注意记得修改路径