| 提示:以下是本篇文章正文内容,下面案例可供参考 第一种常规犯错前端使用get请求,后端未使用注解或注解为@PostMapping或请求路径少/或拦截器路径错误 第二种错误mybatis字段名称与实体类不对应,使用自定义异常捕获,难以察觉.以我的情况:
 前端:
 
  后端:
   
    @PostMapping("/updateSongListPic")
    @ResponseBody
    public Object updateSongListPic(@RequestParam("file") MultipartFile photo, @RequestParam("id") int id) {
        JSONObject jsonObject = new JSONObject();
        if (photo.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "文件上传失败");
            return jsonObject;
        }
        
        String fileName = photo.getOriginalFilename();
        
        
        
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        
        fileName = UUID.randomUUID().toString() + suffixName;
        
        String photoPath = System.getProperty("user.dir") + File.separator + "static" + File.separator
                + "songListImages";
        File file = new File(photoPath);
        
        if (!file.exists()) {
            file.mkdirs();
        }
        
        String finalPath = photoPath + File.separator + fileName;
        
        String storePhotoPath = "/songListImages/" + fileName;
        
        try {
            log.info("hi, log..");
            photo.transferTo(new File(finalPath));
            SongList songList = new SongList();
            songList.setId(id);
            songList.setPicture(storePhotoPath);
            boolean flag = songListService.update(songList);
            if (flag) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "文件上传成功");
                return jsonObject;
            }
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "文件上传失败");
            return jsonObject;
        } catch (Exception e) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "文件上传失败,原因是:" + e.getMessage());
        } finally {
            return jsonObject;
        }
    }
 postman
  debug
 
  可以正常上传文件,但文件不显示.也就是说,可以正常进入代码中,不属于第一种常规错误请注意自定义异常
 
  
 IOException捕获的话前端会报错
  
 Exception捕获的话前端会报错
  
 后端报错
  最后发现
  
 
 |