2015-05-20 50 views
3

我想上傳文件並將其發佈到我的服務器以獲取響應。org.apache.commons.fileupload.FileUploadException:請求被拒絕,因爲沒有找到多部分邊界

在模板中我用類似的東西:

<form name="myForm" enctype="multipart/form-data"> 
     <input type="file" ngf-select ng-model="picFile" name="file" accept="image/*" required> 
     <button class="btn btn-primary" type="submit" ng-click="upload(picFile)" ng-disabled="!myForm.$valid">Ajouter 
    </form> 

這是我的角度控制器和服務:

$scope.upload = function (files) { 
       if (files && files.length) { 
        var file = files[0]; 
        BiAddDocFromExternalSourceService.uploadFile(file); 
     } 
} 
// Service: 
service.uploadFile = function (file) { 
      var fd= new FormData(); 
      fd.append('file', file); 
      return $http.post('ws/bdocument/add_external_document', fd, { 
       transformRequest: angular.identity, 
       headers: { 
        'Content-Type': 'undefined' 
       } 
      }); 
     }; 

而在服務器端,我developped一個REST服務這樣的:

@RequestMapping(value = "/add_external_document", method = RequestMethod.POST) 
public byte[] retrieveBDocumentThumbnail(@RequestParam MultipartFile file) 
     throws AbstractBdocInteractiveException { 
//do something.... 
    return something; 
} 

這是發送到服務器的請求,如果put **'Content-Type':'undefined'** :

Remote Address:127.0.0.1:8080 
Request URL:http://localhost:8080/bdoci- 
tablet/ws/bdocument/add_external_document 
Request Method:POST 
Status Code:500 Erreur Interne de Servlet 
Request Headers 
Accept:application/json, text/plain, */* 
Accept-Encoding:gzip, deflate 
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ar;q=0.2 
Connection:keep-alive 
Content-Length:13520 
Content-Type:undefined 
Cookie:JSESSIONID=25BE1D0F0102A5D3465EFC0644494D71; __ngDebug=true 
Host:localhost:8080 
Origin:http://localhost:8080 
Referer:http://localhost:8080/bdoci-tablet/ 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/39.0.2171.95 Safari/537.36  
Response Headers 
Connection:close 
Content-Language:fr 
Content-Length:4261 
Content-Type:text/html;charset=utf-8 
Date:Wed, 20 May 2015 11:02:20 GMT 
Server:Apache-Coyote/1.1 

,它看起來像如果我把

Remote Address:127.0.0.1:8080 
Request URL:http://localhost:8080/bdoci- 
tablet/ws/bdocument/add_external_document 
Request Method:POST 
Status Code:500 Erreur Interne de Servlet 
Request Headers 
Accept:application/json, text/plain, */* 
Accept-Encoding:gzip, deflate 
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ar;q=0.2 
Connection:keep-alive 
Content-Length:48185 
Content-Type:multipart/form-data 
Cookie:JSESSIONID=EBDEED7F0EAE46677ABD2B2861FEA2A6; __ngDebug=true 
Host:localhost:8080 
Origin:http://localhost:8080 
Referer:http://localhost:8080/bdoci-tablet/ 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, 
like Gecko) Chrome/39.0.2171.95 Safari/537.36 
Response Headers 
Connection:close 
Content-Language:fr 
Content-Length:5266 
Content-Type:text/html;charset=utf-8 
Date:Wed, 20 May 2015 13:12:04 GMT 
Server:Apache-Coyote/1.1 

特林這一點,會引發以下異常:

org.springframework.web.multipart.MultipartException: The current request is not a multipart request

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

回答

1

你需要把它添加到你的Spring bean配置文件:

<bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
</bean> 

An d將其添加到pom.xml設置您想要的版本。

<dependency> 
    <groupId>commons-fileupload</groupId> 
    <artifactId>commons-fileupload</artifactId> 
    <version>1.3.1</version> 
</dependency> 

此外,編輯您的JS服務:

service.uploadFile = function (file) { 
      var fd= new FormData(); 
      fd.append('file', file); 
      return $http.post('ws/bdocument/add_external_document', fd, { 
       transformRequest: angular.identity, 
       headers: { 
        {'Content-Type': undefined} 
        // try with: 
        // {'Content-Type': 'multipart/form-data'} 
        // as well. 
       } 
      }); 
     }; 

你需要讓你的要求多,現在它是 「不確定」。

+0

我用'<豆ID = 「multipartResolver」 \t \t類= 「org.springframework.web.multipart.support.StandardServletMultipartResolver」> \t' 當我改變由你它把我異常開始時服務器:** java.lang.NoClassDefFoundError:org/apache/commons/fileupload/FileItemFactory ** –

+0

我編輯了我的答案,檢查它。 –

+0

我添加了maven依賴關係,並且在啓動服務器時沒有異常,但是仍然拋出** MultipartException ** –

相關問題