springboot+jpa接收实体及文件的上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | @Controller public class Student { @RequestMapping (value= "/admin/student/add" ,method = RequestMethod.GET) public String add(){ return "/admin/student/add" ; } @ResponseBody @RequestMapping (value= "/admin/student/addsave" , method = RequestMethod.POST) public void addsave( @RequestParam (value = "myfile" ) MultipartFile myfile, Students students){ String pic = "" ; if (!myfile.isEmpty()) { String fileName = myfile.getOriginalFilename(); // 文件名 String suffixName = fileName.substring(fileName.lastIndexOf( "." )); // 后缀名 String filePath = "F:/java/uploads/" ; // 上传后的路径 pic = UUID.randomUUID() + suffixName; // 新文件名 File dest = new File(filePath + pic); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } try { myfile.transferTo(dest); } catch (IOException e) { e.printStackTrace(); } } System.out.println(students.getNames()); } } |