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

4

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

在模板中,我使用类似以下方式:

 <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>

这是我的Angular控制器和服务:

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

以下是向服务器发送 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:当前请求不是多部分请求

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

1个回答

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

你需要将请求设置为多部分,目前它是"未定义的"。


我添加了Maven依赖项,并且在启动服务器时没有异常,但仍然抛出了MultipartException - Forgotten Angel
请求怎么样?您可以通过打开浏览器开发者工具,进入网络选项卡并检查请求头/参数来检查它。您能在这里发布吗?您可以编辑第一篇帖子并将其添加到那里。 - Fernando Garcia
发布更新后的请求数据,检查Content-Type:undefined是否未定义或为多部分。 - Fernando Garcia
仍然是同样的错误(500 Servlet内部错误),请求除了Content-Type:multipart/form-data之外没有改变。 - Forgotten Angel
1
你能再写一遍你要发送的请求吗? - Fernando Garcia
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接