Я пытаюсь выбрать файл и загрузить его на FTP. На данный момент я получаю ошибку 415 (тип носителя не поддерживается) при использовании службы springboot в angularjs при отправке изображения.
Это мой угловой контроллер:
Controllers.controller('UploadCtrl', [ '$scope', '$http',
function($scope, $http) {
$scope.doUploadFile = function() {
var file = $scope.uploadedFile;
var url = "/ui/upload";
var data = new FormData();
data.append('uploadfile', file);
var config = {
transformRequest : angular.identity,
transformResponse : angular.identity,
headers : {
'Content-Type' : undefined
}
}
$http.post(url, data, config).then(function(response) {
$scope.uploadResult = response.data;
}, function(response) {
$scope.uploadResult = response.data;
});
};
} ]);
Мой сервисный контроллер JAVA:
@POST
@Path("/upload")
@Consumes("multipart/form-data")
public String upload(@RequestParam("uploadfile") MultipartFile file) throws Exception {
try {
return "You successfully uploaded - " + file.getOriginalFilename();
} catch (Exception e) {
throw new Exception("FAIL! Maybe You had uploaded the file before or the file's size > 500KB");
}
}
А пока просто узнаю имя файла. Что я делаю неправильно при использовании POST?
заранее спасибо


попробуй это:-
<img ng-src = "{{profilePicturePath}}" class = "img-responsive"
alt = "Image Not found" style = "height:150px; width: 150px;">
<input type = "file" class = "form-control" style = "display: none;"
file-model = "profileFile" multiple name = "profileFile"
id = "profileFile" accept = "image/*"/>
$scope.profileFile = null;
$scope.$watch('profileFile', function (profileFile) {
if (profileFile && profileFile.name) {
if (profileFile.size / 1012 < 512) {
if (profileFile.type.startsWith("image/")) {
uploadProfilePicture(profileFile);
}
}
}
});
function uploadProfilePicture(profileFile) {
aspirantService.uploadProfilePicture(profileFile).then(function (response) {
getProfilePicturePath();
});
}
this.uploadProfilePicture = function (file) {
var fd = new FormData();
fd.append("doc", file);
return $http.post(Server.url + 'aspirant/pic/', fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
});
};
@Path("pic")
@POST
@Consumes(MediaType.WILDCARD)
public Response uploadProfilePicture(MultipartFormDataInput input, @Context UserSession session) {
for (Map.Entry<String, List<InputPart>> entry : input.getFormDataMap().entrySet()) {
List<InputPart> list = entry.getValue();
for (Iterator<InputPart> it = list.iterator(); it.hasNext();) {
InputPart inputPart = it.next();
try {
MultivaluedMap<String, String> header = inputPart.getHeaders();
String fileName = RequestHeaderInfo.getFileName(header);
String contentType = RequestHeaderInfo.getContentType(header);
InputStream inputStream = inputPart.getBody(InputStream.class, null);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
//here code for write file
return Response.ok(proxy).build();
} catch (Exception ex) {
logger.log(Level.SEVERE, "Error while getting document!", ex);
}
}
}
return Response.ok().build();
}
я не пробовал ваш код ... но это поможет вам ... он работает в моем случае
Можете ли вы поделиться использованными библиотеками? это рестейз или джерси? или это jax-rs?
Привет спасибо. Я попробую. Как ты считаешь, что не так с тем, что у меня есть?