springboot+jpa接收实体及文件的上传

时间:2020-07-16 22:26:53 类型:JAVA
字号:    

springboot+jpa接收实体及文件的上传

@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());
    }
}


<