@RequestMapping("uploadError") public ModelAndView onUploadError(Locale locale) { ModelAndView modelAndView = new ModelAndView("profile/profilePage"); modelAndView.addObject("error", messageSource.getMessage("upload.file.too.big", null, locale)); modelAndView.addObject("profileForm", userProfileSession.toForm()); return modelAndView; }
@ExceptionHandler(IOException.class) public ModelAndView handleIOException(Locale locale) { ModelAndView modelAndView = new ModelAndView("profile/profilePage"); modelAndView.addObject("error", messageSource.getMessage("upload.io.exception", null, locale)); modelAndView.addObject("profileForm", userProfileSession.toForm()); return modelAndView; }
@RequestMapping(value = "/uploadedPicture") public void getUploadedPicture(HttpServletResponse response) throws IOException { Resource picturePath = userProfileSession.getPicturePath(); if (picturePath == null) { picturePath = anonymousPicture; } response.setHeader( "Content-Type", URLConnection.guessContentTypeFromName(picturePath.getFilename())); IOUtils.copy(picturePath.getInputStream(), response.getOutputStream()); }
@RequestMapping( value = "/profile", params = {"upload"}, method = RequestMethod.POST) public String onUpload(@RequestParam MultipartFile file, RedirectAttributes redirectAttrs) throws IOException { if (file.isEmpty() || !isImage(file)) { redirectAttrs.addFlashAttribute("error", "Incorrect file. Please upload a picture."); return "redirect:/profile"; } Resource picturePath = copyFileToPictures(file); userProfileSession.setPicturePath(picturePath); return "redirect:profile"; }