当前请求不是angularJS和Spring Rest中的多部分请求

当前请求不是angularJS和Spring Rest中的多部分请求,angularjs,spring,rest,Angularjs,Spring,Rest,我试图在客户端使用AngularJS和在服务器端使用SpringRestapi上传文件,但是得到 错误 org.springframework.web.multipart.MultipartException: The current request is not a multipart request at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.assertIsMulti

我试图在客户端使用AngularJS和在服务器端使用SpringRestapi上传文件,但是得到

错误

org.springframework.web.multipart.MultipartException: The current request is not a multipart request
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.assertIsMultipartRequest(RequestParamMethodArgumentResolver.java:216)
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:167)

    .......

[http-bio-8080-exec-1] WARN org.springframework.web.servlet.PageNotFound - Request method 'POST' not supported  
RESTAPI

下面是一个简单的Java Post函数:

@RequestMapping(method = RequestMethod.POST)
public String saveFile(
        @RequestParam("file") MultipartFile file) {
    return "success";
}
在Angular中,我使用资源服务发送请求

Chrome开发者工具输出

Request Payload
------WebKitFormBoundarydFRgXclyfPVixdHo
Content-Disposition: form-data; name="file"; filename="Release_Notes.txt"
Content-Type: text/plain


------WebKitFormBoundarydFRgXclyfPVixdHo--
角度服务

  function FileUploadService($resource) {
    return $resource('/fileUpload/:id', {}, {
        'save' : {
            method : 'POST',
            transformRequest: function(data, headersGetter) {
                var headers = headersGetter();
                headers['Content-Type'] = undefined;

                if (data == undefined) {
                  return data;
                }

                var fd = new FormData();

                var createKey = function(_keys_, currentKey) {
                  var keys = angular.copy(_keys_);
                  keys.push(currentKey);
                  var formKey = keys.shift()

                  if (keys.length) {
                    formKey += "[" + keys.join("][") + "]"
                  }

                  return formKey;
                };

                var addToFd = function(object, keys) {
                  angular.forEach(object, function(value, key) {
                    var formKey = createKey(keys, key);

                    if (value instanceof File) {
                      fd.append(formKey, value);
                    } else if (value instanceof FileList) {
                      if (value.length == 1) {
                        fd.append(formKey, value[0]);
                      } else {
                        angular.forEach(value, function(file, index) {
                          fd.append(formKey + '[' + index + ']', file);
                        });
                      }
                    } else if (value && (typeof value == 'object' || typeof value == 'array')) {
                      var _keys = angular.copy(keys);
                      _keys.push(key)
                      addToFd(value, _keys);
                    } else {
                      fd.append(formKey, value);
                    }
                  });
                };

                addToFd(data, []);

                return fd;
              }
          }
        });
  }

任何避免此错误的提示?

方法断言调用RequestParamMethodArgumentResolver类的MultipartRequest

该方法断言它是一个post请求,内容类型以multipart开头/

if (!"post".equals(request.getMethod().toLowerCase())) {
        return false;
}
String contentType = request.getContentType();
return (contentType != null && contentType.toLowerCase().startsWith("multipart/"));
另一方面,您的内容类型是

Content-Type: text/plain

并引发异常。

方法断言调用RequestParamMethodArgumentResolver类的MultipartRequest

@RequestMapping(method = RequestMethod.POST)
该方法断言它是一个post请求,内容类型以multipart开头/

if (!"post".equals(request.getMethod().toLowerCase())) {
        return false;
}
String contentType = request.getContentType();
return (contentType != null && contentType.toLowerCase().startsWith("multipart/"));
另一方面,您的内容类型是

Content-Type: text/plain
然后抛出一个异常

@RequestMapping(method = RequestMethod.POST)
requestmapping中缺少value属性,应该是这样的

@RequestMapping(value="/fileupload/save/{id}" ,method = RequestMethod.POST)
并在创建角度资源时使用此代码

$resource('fileupload/save/:id',
      {id:'1'}, {
      save: {method:'POST', params:{charge:true}}
      });
在springBoot中,上传文件时没有太多的配置。 但您可以将这些属性添加到应用程序属性文件中,以更改文件大小限制

# File size limit
multipart.maxFileSize = 3Mb

# Total request size for a multipart/form-data
multipart.maxRequestSize = 20Mb
requestmapping中缺少value属性,应该是这样的

@RequestMapping(value="/fileupload/save/{id}" ,method = RequestMethod.POST)
并在创建角度资源时使用此代码

$resource('fileupload/save/:id',
      {id:'1'}, {
      save: {method:'POST', params:{charge:true}}
      });
在springBoot中,上传文件时没有太多的配置。 但您可以将这些属性添加到应用程序属性文件中,以更改文件大小限制

# File size limit
multipart.maxFileSize = 3Mb

# Total request size for a multipart/form-data
multipart.maxRequestSize = 20Mb

上述问题通过以下方式解决:

1) 在WebAppConfig.java中创建多部分解析器bean,如下所示:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
}
$http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        });
2) 将AngularJS FileUploadService(正在使用资源服务)替换为http,如下所示:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
}
$http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        });

希望有帮助。

上述问题通过以下方式解决:

1) 在WebAppConfig.java中创建多部分解析器bean,如下所示:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
}
$http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        });
2) 将AngularJS FileUploadService(正在使用资源服务)替换为http,如下所示:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
}
$http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        });

希望有帮助。

你能展示你的rest-servlet.xml吗?我的项目中没有这样的文件。好的,app-context.xml呢?这应该在那里,因为你正在使用弹簧!你能显示你的rest-servlet.xml吗?我的项目中没有这样的文件。好的,app-context.xml呢?这应该在那里,因为你正在使用弹簧!我们的项目中没有使用SpringBoot。这是一个基于maven的SpringMVC项目。关于value属性,在类级别,我将其定义为@RequestMapping(value=“/api/v1/fileUpload”),我们在项目中不使用Spring引导。这是一个基于maven的SpringMVC项目。关于value属性,在类级别,我将其定义为@RequestMapping(value=“/api/v1/fileUpload”),看起来您正在这里设置它:headers['Content-Type']=undefined;尝试将其设置为多部分/表单数据;看起来您正在这里设置它:headers['Content-Type']=未定义;尝试将其设置为多部分/表单数据;