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

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

这是我的 Angular Controller 和服务:

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

这是发送到服务器的请求,如果输入 ** '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

最佳答案

您需要将此添加到您的 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.
                }
            });
        };

你需要让你的请求多部分,现在它是“未定义的”。

关于java - org.apache.commons.fileupload.FileUploadException : the request was rejected because no multipart boundary was found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30347126/

相关文章:

java - Apache Ignite 中客户端的内存指标

java - 从 apigee api 代理 : Class is not a function, 中的 javascript 创建 java 对象它是对象

javascript - 在不触发 hashchange 事件的情况下以编程方式更新哈希?

javascript - 从文件中 Angular 获取文本区域

java - 通过 TextArea 中的 JButton 删除单行

java - 无锁 CAS 混淆

javascript - 如何在纯js中克隆图像

javascript - 谷歌地图多边形显示/隐藏切换与复选框

javascript - 将作用域传递给函数不起作用

javascript - 单击后折叠导航栏保持打开状态