2016-09-19 92 views
1

我想上传使用在服务器端对客户端和春天RESTAPI AngularJS但得到目前的请求不angularJS和弹簧安置multipart请求

文件错误

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 

Rest API

下面是一个简单的Java Post功能:

@RequestMapping(method = RequestMethod.POST) 
public String saveFile(
     @RequestParam("file") MultipartFile file) { 
    return "success"; 
} 

在Angular中,我使用Resource服务发送请求。

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

任何暗示,以避免这个错误?

+0

你可以显示你的rest-servlet.xml吗? –

+0

我的项目中没有这样的文件。 – Moazzam

+0

好的app-context.xml怎么样?这应该在那里,因为你正在使用弹簧! –

回答

0

上述问题是由解决

@Bean 
public MultipartResolver multipartResolver() { 
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); 
    return multipartResolver; 
} 

2)更换AngularJS FileUploadService(这是使用资源服务)与http如下所示:

$http.post(uploadUrl, fd, { 
      transformRequest: angular.identity, 
      headers: {'Content-Type': undefined} 
     }); 

希望它有帮助。

0

调用RequestParamMethodArgumentResolver类的方法assertIsMultipartRequest。

方法,断言它是一个POST请求和内容类型与多开始/

if (!"post".equals(request.getMethod().toLowerCase())) { 
     return false; 
} 
String contentType = request.getContentType(); 
return (contentType != null && contentType.toLowerCase().startsWith("multipart/")); 

你的内容类型,在另一方面,是

Content-Type: text/plain 

而且会抛出异常。创建角资源时

+0

如何设置正确的Content-Type? – Moazzam

+0

看起来像你在这里设置: 标题['Content-Type'] =未定义;尝试将其设置为multipart/form-data; – Janar

0
@RequestMapping(method = RequestMethod.POST) 

你的价值属性在requestmapping它应该是这样的

@RequestMapping(value="/fileupload/save/{id}" ,method = RequestMethod.POST) 

,并使用此代码所缺少

$resource('fileupload/save/:id', 
     {id:'1'}, { 
     save: {method:'POST', params:{charge:true}} 
     }); 
在springBoot

那里有没有太大的情况下配置上传文件。 但您可以将这些属性添加到您的应用程序属性文件以更改文件大小限制。

1)创建在WebAppConfig.java一个MultipartResolver豆,如下所示::

# File size limit 
multipart.maxFileSize = 3Mb 

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

我们在我们的项目中没有使用Spring Boot。这是一个基于maven的spring mvc项目。关于值属性,在类级别,我已将其定义为** @ RequestMapping(value =“/ api/v1/fileUpload”)** – Moazzam