IDEA文件上传

1.首先在yml中配置文件上传大小

 sevlert:
    multipart:
       max-file-size: 50MB

2.再在xml中配置


        commons-fileupload
        commons-fileupload
    

3.创建html文件


4.创建FileUploadController (返回不跳转页面)

@Controller
public class FileUploadController {
@RequestMapping("upload")
public String upload(HttpServletRequest request, MultipartFile file) throws Exception{
    //如果文件不为空
    if(!file.isEmpty()){
        String path = request.getServletContext().getRealPath("/upload");
        System.out.println(path);

        //获取文件名
        String filename = file.getOriginalFilename();
        File file1 = new File(path,filename);
        System.out.println("!!!!"+file1.getParentFile());
        if(!file1.getParentFile().exists()){
            file1.getParentFile().mkdirs();
        }
        file.transferTo(file1);
         return  "index";

     }

    return  "index";
}

}

你可能感兴趣的:(IDEA文件上传)