org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界

时间:2015-05-20 10:34:47

标签: java javascript angularjs spring spring-mvc

我想上传文件并将其发布到我的服务器以获取响应。

在模板中我使用类似的东西:

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

在服务器端,我开发了类似的REST服务:

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

以下是发送给服务器的请求**如果内容类型&#39;:未定义&#39; **:

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:当前请求不是多部分请求

     

org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界

1 个答案:

答案 0 :(得分:1)

您需要将此添加到spring beans配置文件中:

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

并将其添加到设置所需版本的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.
                }
            });
        };

您需要将您的请求设置为多部分,现在它是&#34;未定义&#34;。