@ResponseBody @RequestMapping(value = "/upload",method = RequestMethod.POST) public Map<String,Object> upload(HttpServletRequest request, MultipartFile file) throws IOException { Map<String,Object> map=new HashMap<>(); //如果文件不为空,写入上传路径 if(!file.isEmpty()) { //上传文件路径 String path = "/Users/weirdor/zhy/aop_logging/img"; //上传文件名 String filename = file.getOriginalFilename(); //获取上传文件的后缀名 String prefix=filename.substring(filename.lastIndexOf(".")+1); //通过uuid来设置文件名 filename= UUID.randomUUID()+"."+prefix; File filepath = new File(path,filename); //判断路径是否存在,如果不存在就创建一个 if (!filepath.getParentFile().exists()) { filepath.getParentFile().mkdirs(); } //将上传文件保存到一个目标文件当中 file.transferTo(new File(path + File.separator + filename)); map.put("code",0); } else { map.put("code",1); } return map; }
spring mvc文件上传
最新推荐文章于 2024-07-19 17:50:16 发布