首页 > 解决方案 > 带有formdata和其他字段控制的http post不会转到java

问题描述

我是 AngularJs 的新手。我正在尝试通过 Jax-RS post rest API 上传图像文件(.bmp、.jpg 等)及其标签,但我控制不从我的 angularjs 控制器进入 java post api。

调试时控件没有进入 java 文件。您能否帮助理解我的代码中有什么问题。

我的文件.html

Label of File: <input type="text" name="iconlabel" data-ng-model="imporLabelName"><br>
Import a File: <input type="file" id="myFile" name="myfile" data-file-model="myfile"><br>
<button type="button" class="btn btn-primary pull-right" data-ng-click="uploadfile();">Submit
                </button>

我的文件控制器

define([ '{angular}/angular' ], function(angular) {
var module = angular.module('myFile', [ 'ngResource', 'colorpicker-dr' ]);

module.controller('myFileController', [ '$scope', '$sce', '$http', '$location', '$window', 'w20MessageService'
,function($scope, $sce, $http, $location, $window, w20MessageService) {
    
            var config = { 
                    headers: {
                        "Content-Type": undefined,
                    }
                };
       
            /*** Modale for MyFile **/
            $scope.openMyFile = function() {
                $("#myfile").modal("show");
            };
            
            $scope.uploadfile = function() {
                $scope.file = document.getElementById('myFile').files[0];
                alert('LabelName = '+$scope.imporLabelName);
                
                var formData = new $window.FormData();
                formData.append("label", $scope.imporLabelName);
                formData.append("file", $scope.file);
                alert('File = '+$scope.file);
                var url = "uploadfile/label="+$scope.imporLabelName;
                alert("URL = "+url);
                $http.post(url,$scope.formData,config).success(function(response) {
                    $scope.result = "SUCCESS";
                }).error(function(response, status) {
                    if (status === 400) {
                        w20MessageService.addMessageErreur(data.message, "msgGererParam");
                    } else {
                        w20MessageService.addMessageErreur("eox.message.error.load.traitement", "msgGererParam");
                    }
                });
              $("#myfile").modal("hide");
            };
        } ]);
return {
    angularModules : [ 'digitalJes' ]
};
});

java API 代码

@POST
@Path("/uploadfile/{label}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(@PathParam("label") String label, @FormDataParam("file") InputStream fileInputStream,
        @FormDataParam("file") FormDataContentDisposition fileInputDetails) {

    CacheControl cc = new CacheControl();
    cc.setNoCache(true);
    return Response.ok(uploadFileUtil.uploadFile(label, fileInputStream, fileInputDetails)).cacheControl(cc).build();
}

错误代码和错误信息

HTTP 状态 400 – 错误请求

由于某些被认为是客户端错误(例如,格式错误的请求语法、无效的请求消息帧或欺骗性请求路由),服务器不能或不会处理请求。

标签: javascriptjavaangularjsrestangularjs-scope

解决方案


 @FormDataParam("file") InputStream fileInputStream,
 @FormDataParam("file") FormDataContentDisposition fileInputDetails

似乎fileInputStreamfileInputDetails变量都是从file参数初始化的。html该领域还有

...
Import a File: <input type="file" id="myFile" name="myfile" data-file-model="myfile"><br>

在这里id="myFile"name="myfile"并且您将这些作为"@FormDataParam("file")"接收。


推荐阅读