我很清楚有很多问题与这个问题相同,但是由于我的HTML文件名与JavaScript和Java中的文件名相同,因此没有一个问题可以帮助我。这是JavaScript代码:
var fd = new FormData();
fd.append('firstName', $scope.user.firstName);
fd.append('lastName', $scope.user.lastName);
fd.append('phoneNumber', $scope.user.phoneNumber);
fd.append('email', $scope.user.email);
fd.append('username', $scope.user.username);
fd.append('password', $scope.user.password);
fd.append('file', $scope.user.profilePicture);
fd.append('file', $scope.user.profilePicture.name);
console.log("Image:" + $scope.user.profilePicture);
console.log("Image name:" + $scope.user.profilePicture.name);
当我console.log("Image name:" + $scope.user.profilePicture.name);
一切似乎都很好,但是当我发送请求时,我得到null
。这是Java代码:
@Path("/register")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response registerUser(
@FormDataParam("firstName") String firstName,
@FormDataParam("lastName") String lastName,
@FormDataParam("phoneNumber") String phoneNumber,
@FormDataParam("email") String email,
@FormDataParam("username") String username,
@FormDataParam("password") String password,
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDescription,
@Context ServletContext servletContext) throws JsonGenerationException, JsonMappingException, IOException {
String uploadedFileLocation = servletContext.getRealPath("/resources/images")+"/"+fileDescription.getFileName();
writeToFile(uploadedInputStream, uploadedFileLocation);
String imgPath = "http://localhost:8080/WebProjekat/resources/images/" + fileDescription.getFileName();
User user = new User(username, password,firstName,lastName,phoneNumber,email,imgPath);
if(userService.addUser(user)) {
return Response.status(Status.CREATED)
.entity(user)
.build();
}else
return Response.status(Status.NOT_ACCEPTABLE)
.entity(user)
.build();
}
以某种方式FormDataContentDisposition fileDescription始终为null。这是HTML代码:
<div class="form-group">
<img style="max-height: 250px; max-width: 200px;" src="app/image_resources/guest.png" id="profileImg"></img>
<input type="button" class="btn btn-primary" style="width: 100%;height: 30px; display: flex;align-items: center;justify-content: center;" value="Load Image" onclick="$('#loadProfileImg').click();" />
<input type="file" accept="image/*" class="form-control" id="loadProfileImg" ng-required="true" onchange="angular.element(this).scope().readURL(this);" style="display:none;" name="file" />
如你所见,我已经命名
<input type='file'>
和我在JavaScript中的密钥一样。我还在我的web.xml中包含了这个:
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
任何形式的帮助将不胜感激。提前谢谢你们!
UPDATE :我已修复了null问题,我意外地写道:
fd.append('file', $scope.user.profilePicture);
fd.append('file', $scope.user.profilePicture.name);
而不是:
fd.append('file', $scope.user.profilePicture, $scope.user.profilePicture.name);
但遗憾的是我有另一个问题。现在我的img路径是正确的,但我得到404 NOT FOUND,即使我看到它要上传的文件夹中的图像名称,但我看不到图像本身。我在2个不同的对象上使用此代码,它工作正常。有什么想法吗?
答案 0 :(得分:0)
阅读网址附加表单数据。
<input type="file" class="form-control" id="file" ng-model="file"
onchange="angular.element(this).scope().getTheFiles(this)" />
var formdata = new FormData();
$scope.getTheFiles = function(element) {
$scope.$apply(function($scope) {
$scope.files = element.files;
for (var i = 0; i < element.files.length; i++) {
formdata.append('files', element.files[i]);
}
});
};
Java端获取类似@FormDataParam("files") List<FormDataBodyPart>
个文件的值。