springboot+jpa接收實(shí)體及文件的上傳
@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()); } }