Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @PostMapping("/{id}/profile-picture/upload")
- @PreAuthorize("isAuthenticated()")
- public String uploadPicture(@PathVariable Integer id, FileBindingModel fileBindingModel) {
- if (!this.userRepository.exists(id)) {
- return "redirect:/profile";
- }
- UserDetails principal = (UserDetails) SecurityContextHolder.getContext()
- .getAuthentication().getPrincipal();
- User loggedUser = this.userRepository.findByEmail(principal.getUsername());
- User user = this.userRepository.findOne(id);
- if (!loggedUser.equals(user)) {
- return "redirect:/login";
- }
- MultipartFile file = fileBindingModel.getPicture();
- if (file != null) {
- String originalFileName = file.getOriginalFilename();
- File imageFile = new File
- ("D:\\javaBroadcast\\newsWebsiteProject\\blog\\src\\main\\resources\\static\\images\\", originalFileName);
- try {
- file.transferTo(imageFile);
- user.setImagePath(imageFile.getPath());
- this.userRepository.saveAndFlush(user);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment