@PostMapping("/upload") public ResultVo upload( @RequestParam("file") MultipartFile file,Map paramMap) throws IOException { ResultVo resultVo = new ResultVo(); System.out.println("准备上传"); if (file.isEmpty()) { throw new RuntimeException("上传的文件不能为空"); } //获取原始文件名称 String originalFilename = file.getOriginalFilename(); //获取文件后缀名 String extension = "." + FilenameUtils.getExtension(originalFilename); //.jpg //获取新文件名称 命名:去掉上传的后缀名 String newFileName = originalFilename.substring(0,originalFilename.lastIndexOf(".")); //获取资源路径 classpath的项目路径+/static/files classpath就是resources的资源路径 String path = ResourceUtils.getURL("classpath:").getPath() + "static/report/"; //新建一个时间文件夹标识,用来分类 String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); //全路径(存放资源的路径) 资源路径+时间文件夹标识 String dataDir = path + format; //全路径存放在文件类中,判断文件夹是否存在不存在就创建 File filePath = new File("C:/desktop/"); //也可以直接放进去进行拼接 File dataFile = new File(path,format); if (!filePath.exists()) { filePath.mkdirs(); } paramMap.put("name",newFileName); //判断数据库里面是不是有这个文件存在,存在则不允许继续上传 List<ReportTemplate> templateNames = rptTemplateService.getTemplateName(paramMap); if (templateNames.size()>0){ throw new RuntimeException("该报表模板名称已存在,请再选上传文件"); }else{ //文件上传至指定路径 file.transferTo(new File("C:/desktop/", newFileName)); //new一个对象,把需要的参数放到map里面传给前端 Map<String,Object> data = new HashMap<String,Object>(); data.put("templateFilePath",filePath); data.put("name",newFileName); resultVo.setData(data); System.out.println("上传结束"); return resultVo; } }