从angular js发送multiparts文件到java jersey

时间:2017-01-24 09:08:39

标签: javascript java angularjs jersey-2.0

我有这个客户端(角度js):

self.onSubmit = function(){
                var formData = new FormData();
                angular.forEach($scope.files,function(obj){
                    if(!obj.isRemote){
                        formData.append('files[]', obj.lfFile);
                    }
                });


                uploadService.upload(formData);
            };

  var upload = function (files) {
            var deferred = $q.defer();

            $http.post('http://localhost:8082/api/VoicesUploader/uploadFiles', files,
                {
                    transformRequest: angular.identity,
                    headers: {'Content-Type': undefined}}
            ).then(
                function successCallback(response) {
                    lastResults = response.data;
                    deferred.resolve(response.data);
                }, function errorCallback(response) {
                    console.log(response);
                    lastResults = [];
                    deferred.reject("failed to upload");

                });

            return deferred.promise;
        };

服务器端(泽西岛,java):

 @Path("/uploadFiles")
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFiles(@DefaultValue("false") @FormDataParam("allowOverride") boolean allowOverride,
                                @DefaultValue("") @FormDataParam("voiceId") String voiceId,
                                @FormDataParam("files") List<FormDataBodyPart> bodyParts,
                                @FormDataParam("files") FormDataContentDisposition fileDispositions) {

但我在服务器端得到bodyParts == null

我错过了什么?

更新

我将字段名称更改为@peeskillet建议,

formData.append('files', obj.lfFile);

但是我收到了一个错误:

Request URL:http://localhost:8082/api/VoicesUploader/uploadFiles
Request Method:POST
Status Code:400 Bad Request
Remote Address:[::1]:8082
Response Headers
view source
Cache-Control:must-revalidate,no-cache,no-store
Content-Length:351
Content-Type:text/html;charset=iso-8859-1
Date:Wed, 25 Jan 2017 10:10:09 GMT
Server:Jetty(9.3.15.v20161220)
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,he;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Length:44
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryyAAB8aXeTPl7BoI6
Cookie:Idea-1bcbd00d=8ec84137-c179-4dea-821b-6a3ed498eacf; JSESSIONID_LOCAL=28D25CAF06D4CA4DABFA313216ACC614; stToken=${{secret}}; Idea-1bcbd00e=0b993a44-caee-4794-9d9f-a01cf60427fe; io=IMD9JVeEeX3hqPwgAAAA
Host:localhost:8082
Origin:http://localhost:8082
Pragma:no-cache
Referer:http://localhost:8082/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36

Request Payload
------WebKitFormBoundaryyAAB8aXeTPl7BoI6--

0 个答案:

没有答案