2016-09-19 40 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