带有表单数据和其他字段控件的 http post 不会 java
http post with formdata and other field control not going to java
我是 AngularJs 的新手。我正在尝试通过 Jax-RS post rest API 上传图像文件(.bmp、.jpg 等)及其标签,但我无法控制 java post api 来自我的 angularjs 控制器。
调试时控件未进入 java 文件。您能否帮助理解我的代码中的错误。
myfile.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' ]
};
});
javaapi代码
@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 Status 400 – Bad Request
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileInputDetails
似乎 fileInputStream
和 fileInputDetails
变量都从 file
参数初始化。同样在 html
中,该字段是
...
Import a File: <input type="file" id="myFile" name="myfile" data-file-model="myfile"><br>
这里 id="myFile" 和 name="myfile" 你收到的是 "@FormDataParam("file")".
我是 AngularJs 的新手。我正在尝试通过 Jax-RS post rest API 上传图像文件(.bmp、.jpg 等)及其标签,但我无法控制 java post api 来自我的 angularjs 控制器。
调试时控件未进入 java 文件。您能否帮助理解我的代码中的错误。
myfile.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' ]
};
});
javaapi代码
@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 Status 400 – Bad Request
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileInputDetails
似乎 fileInputStream
和 fileInputDetails
变量都从 file
参数初始化。同样在 html
中,该字段是
...
Import a File: <input type="file" id="myFile" name="myfile" data-file-model="myfile"><br>
这里 id="myFile" 和 name="myfile" 你收到的是 "@FormDataParam("file")".