springboot ajax文件上传

前端

ajax

function submitFunction() {
	    var url = "..........";
        var formData = new FormData($("#form-add")[0]);
	    $.ajax({
			type: "post",
			url: url,
            data : formData,
            cache: false,			 //不需要缓存
            processData : false,  	//必须false才会避开jQuery对 formdata 的默认处理
            contentType : false, 	 //必须false才会自动加上正确的Content-Type
			success:function(data){
				
			},
			error:function(){
				alert("请求出错!");
			}
		});
	}

后端
application.yml

	## 文件大小配置
	spring:
	  servlet:
	    multipart:
	      max-file-size: 20MB	  #单个文件带下
	      max-request-size: 20MB	#总上传数据大小

controller

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public void uploadFile(HttpServletResponse response, HttpServletRequest request,
               @RequestParam MultipartFile file,
               @RequestParam String name) throws IOException {
        System.out.println(file.isEmpty());
        // 判断是否为空
        /*if (file != null && !file.isEmpty() && file.getSize()!=0) {
            // 上传文件、request请求、保存目录(upload/规范文章管理)、路径前缀
            String filePath = saveFile(file, request, UPLOAD_FILE_PATH, null);
            System.out.println(filePath);
        }*/
    }

你可能感兴趣的:(java)