// TODO:cannot process MultipartException @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; }
// TODO:cannot process MultipartException @ExceptionHandler(MultipartException.class) public ModelAndView onUploadError1(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 = "/profile", params = "upload", method = RequestMethod.POST) public String onUpload(MultipartFile file, RedirectAttributes redirectAttrs, Model model) throws IOException { if (file.isEmpty() || !isImage(file)) { // throw new IOException("Incorrect file.Please upload a picture."); redirectAttrs.addFlashAttribute("error", "Incorrect file.Please upload a picture."); return "redirect:/profile"; } // copyFileToPictures(file); Resource picturePath = copyFileToPictures(file); // model.addAttribute("picturePath", picturePath.getFile().toPath()); userProfileSession.setPicturePath(picturePath); return "redirect:/profile"; }
@RequestMapping(value = "/uploadedPicture") public void getUploadedPicture(HttpServletResponse response) throws IOException { // ClassPathResource classPathResource = new // ClassPathResource("/images/anonymous.jpg"); // response.setHeader("Content-Type", // URLConnection.guessContentTypeFromName(classPathResource.getFilename())); // IOUtils.copy(classPathResource.getInputStream(), // response.getOutputStream()); // response.setHeader("Content-Type", // URLConnection.guessContentTypeFromName(anonymousPicture.getFilename())); // IOUtils.copy(anonymousPicture.getInputStream(), // response.getOutputStream()); Resource picturePath = userProfileSession.getPicturePath(); if (picturePath == null) { picturePath = anonymousPicture; } response.setHeader( "Content-Type", URLConnection.guessContentTypeFromName(picturePath.toString())); IOUtils.copy(picturePath.getInputStream(), response.getOutputStream()); }